iPad/iOS 5+;:阿拉伯语和其他非拉丁语(A-Z字符)语言的UITableViewCell内部UITextView中的行距错误

iPad/iOS 5+;:阿拉伯语和其他非拉丁语(A-Z字符)语言的UITableViewCell内部UITextView中的行距错误,ios,uitableview,uitextview,arabic,Ios,Uitableview,Uitextview,Arabic,iOS5+for iPad中存在一个非常奇怪的行距错误:如果您使用的UITableView中的每个UITableViewCell都是包含UITextView的自定义类型,那么在模式视图中打开键盘后,包含来自非a-Z语言(如阿拉伯语)的多行文本的单元格将以错误的行距排在队列中 注意:虽然这似乎是iOS中的一个bug,但我正在寻找一个解决方法,使我的应用程序能够向后兼容iOS5+版本 下面是如何复制错误: (1)(a)在情节提要中,创建一个UIViewController(尽管创建自己的子类),其中

iOS5+for iPad中存在一个非常奇怪的行距错误:如果您使用的UITableView中的每个UITableViewCell都是包含UITextView的自定义类型,那么在模式视图中打开键盘后,包含来自非a-Z语言(如阿拉伯语)的多行文本的单元格将以错误的行距排在队列中

注意:虽然这似乎是iOS中的一个bug,但我正在寻找一个解决方法,使我的应用程序能够向后兼容iOS5+版本

下面是如何复制错误:

(1)(a)在情节提要中,创建一个UIViewController(尽管创建自己的子类),其中包含一个UIToolbar和一个UITableView。在UITableView中,创建一个标识符为“CellIdentifier”的“Dynamic Prototype”单元和一个标记为“1”且所有AutoResize属性处于启用状态的UITextView(因此它占用单元的全部大小)

(1)(b)在故事板中,将UIToolbar链接到“toolbar”变量,将UITableView链接到“mainTableView”变量。将UITableView“数据源”和“委托”设置为UIViewController,并将UITextView“委托”设置为UIViewController

(1)(c)向UIViewController子类添加以下代码:

MyViewController.h:

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextViewDelegate>

// the toolbar at the top of the screen
@property (weak, nonatomic) IBOutlet UIToolbar *mainToolbar;

// the table view taking up the screen
@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@end
#import "MyViewController.h"

@interface MyViewController ()

// array to store text for cells
@property (strong, nonatomic) NSMutableArray *data;

// the number of rows to show
@property (nonatomic) NSInteger numberOfRowsToShow;

// when toolbar buttons are pressed
- (void)showModalViewButtonPressed;
- (void)redrawVisibleCellsButtonPressed;
- (void)insertButtonPressed;
- (void)deleteButtonPressed;

@end

@implementation MyViewController

@synthesize mainTableView;
@synthesize mainToolbar;
@synthesize data;

@synthesize numberOfRowsToShow;

- (void)viewDidLoad {
    [super viewDidLoad];
    // create data array for cell contents
    self.data = [[NSMutableArray alloc] initWithObjects:
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 nil];
    self.numberOfRowsToShow = 0;
    // set toolbar buttons
    [self.mainToolbar setItems:[NSArray arrayWithObjects:
                                [[UIBarButtonItem alloc] initWithTitle:@"Show Modal View" style:UIBarButtonItemStyleBordered target:self action:@selector(showModalViewButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Redraw Visible Cells" style:UIBarButtonItemStyleBordered target:self action:@selector(redrawVisibleCellsButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Insert" style:UIBarButtonItemStyleBordered target:self action:@selector(insertButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(deleteButtonPressed)],
                                nil] animated:YES];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // clean up
    self.data = nil;
    self.mainTableView = nil;
    self.mainToolbar = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // only one section
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // return the number of items in the data array
    return self.numberOfRowsToShow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // dequeue cell from storyboard
    UITableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    // get text view
    UITextView *cellText = (UITextView *)[cell viewWithTag:1];
    // set text using data array
    cellText.text = [self.data objectAtIndex:indexPath.row];
    // set large font
    cellText.font = [UIFont systemFontOfSize:20.0f];
    // set background color based on whether the line spacing is correct
    if (cellText.contentSize.height == 100.0f) {
        // correct height sets a green background
        cellText.backgroundColor = [UIColor greenColor];
    } else if (cellText.contentSize.height == 88.0f) {
        // wrong height sets a red background
        cellText.backgroundColor = [UIColor redColor];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // return constant height
    return 150.0f;
}

- (void)textViewDidChange:(UITextView *)textView {
    // get parent cell for text view
    UITableViewCell *parentCell = (UITableViewCell *)textView.superview.superview;
    // get parent cell indent path
    NSIndexPath *parentIndexPath = [self.mainTableView indexPathForCell:parentCell];
    // save new text to data array
    [self.data replaceObjectAtIndex:parentIndexPath.row withObject:textView.text];
}

#pragma mark - Other methods

- (void)showModalViewButtonPressed {
    // segue to modal view
    [self performSegueWithIdentifier:@"modalSegue" sender:self];
}

- (void)redrawVisibleCellsButtonPressed {
    // reload all visible cells in the table view
    [self.mainTableView reloadRowsAtIndexPaths:[self.mainTableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)insertButtonPressed {
    // insert a cell in the table view (increments number of rows to show first)
    [self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.numberOfRowsToShow++ inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)deleteButtonPressed {
    // delete a cell in the table view (decrements number of rows to show first)
    [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:--self.numberOfRowsToShow inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

@end
#import <UIKit/UIKit.h>

@interface MyModalViewController : UIViewController

- (IBAction)closeButtonPressed:(id)sender;

@end
#import "MyModalViewController.h"

@implementation MyModalViewController

- (IBAction)closeButtonPressed:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

@end
(2)(a)在情节提要中,创建UINavigationController和UIViewController(也将其子类化),并在其属性中将UIViewController设置为UINavigationController的根视图控制器。将带有“完成”按钮的UIToolbar以及UITextField添加到UIViewController。将模态segue添加到UINavigationController主视图控制器“modalSegue”

(2)(b)在故事板中,将“完成”按钮与iAction“closeButtonPressed”链接起来

(2)(c)将以下代码添加到模态UIViewController:

MyModalViewController.h:

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextViewDelegate>

// the toolbar at the top of the screen
@property (weak, nonatomic) IBOutlet UIToolbar *mainToolbar;

// the table view taking up the screen
@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@end
#import "MyViewController.h"

@interface MyViewController ()

// array to store text for cells
@property (strong, nonatomic) NSMutableArray *data;

// the number of rows to show
@property (nonatomic) NSInteger numberOfRowsToShow;

// when toolbar buttons are pressed
- (void)showModalViewButtonPressed;
- (void)redrawVisibleCellsButtonPressed;
- (void)insertButtonPressed;
- (void)deleteButtonPressed;

@end

@implementation MyViewController

@synthesize mainTableView;
@synthesize mainToolbar;
@synthesize data;

@synthesize numberOfRowsToShow;

- (void)viewDidLoad {
    [super viewDidLoad];
    // create data array for cell contents
    self.data = [[NSMutableArray alloc] initWithObjects:
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 nil];
    self.numberOfRowsToShow = 0;
    // set toolbar buttons
    [self.mainToolbar setItems:[NSArray arrayWithObjects:
                                [[UIBarButtonItem alloc] initWithTitle:@"Show Modal View" style:UIBarButtonItemStyleBordered target:self action:@selector(showModalViewButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Redraw Visible Cells" style:UIBarButtonItemStyleBordered target:self action:@selector(redrawVisibleCellsButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Insert" style:UIBarButtonItemStyleBordered target:self action:@selector(insertButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(deleteButtonPressed)],
                                nil] animated:YES];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // clean up
    self.data = nil;
    self.mainTableView = nil;
    self.mainToolbar = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // only one section
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // return the number of items in the data array
    return self.numberOfRowsToShow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // dequeue cell from storyboard
    UITableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    // get text view
    UITextView *cellText = (UITextView *)[cell viewWithTag:1];
    // set text using data array
    cellText.text = [self.data objectAtIndex:indexPath.row];
    // set large font
    cellText.font = [UIFont systemFontOfSize:20.0f];
    // set background color based on whether the line spacing is correct
    if (cellText.contentSize.height == 100.0f) {
        // correct height sets a green background
        cellText.backgroundColor = [UIColor greenColor];
    } else if (cellText.contentSize.height == 88.0f) {
        // wrong height sets a red background
        cellText.backgroundColor = [UIColor redColor];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // return constant height
    return 150.0f;
}

- (void)textViewDidChange:(UITextView *)textView {
    // get parent cell for text view
    UITableViewCell *parentCell = (UITableViewCell *)textView.superview.superview;
    // get parent cell indent path
    NSIndexPath *parentIndexPath = [self.mainTableView indexPathForCell:parentCell];
    // save new text to data array
    [self.data replaceObjectAtIndex:parentIndexPath.row withObject:textView.text];
}

#pragma mark - Other methods

- (void)showModalViewButtonPressed {
    // segue to modal view
    [self performSegueWithIdentifier:@"modalSegue" sender:self];
}

- (void)redrawVisibleCellsButtonPressed {
    // reload all visible cells in the table view
    [self.mainTableView reloadRowsAtIndexPaths:[self.mainTableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)insertButtonPressed {
    // insert a cell in the table view (increments number of rows to show first)
    [self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.numberOfRowsToShow++ inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)deleteButtonPressed {
    // delete a cell in the table view (decrements number of rows to show first)
    [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:--self.numberOfRowsToShow inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

@end
#import <UIKit/UIKit.h>

@interface MyModalViewController : UIViewController

- (IBAction)closeButtonPressed:(id)sender;

@end
#import "MyModalViewController.h"

@implementation MyModalViewController

- (IBAction)closeButtonPressed:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

@end
测试:

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextViewDelegate>

// the toolbar at the top of the screen
@property (weak, nonatomic) IBOutlet UIToolbar *mainToolbar;

// the table view taking up the screen
@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@end
#import "MyViewController.h"

@interface MyViewController ()

// array to store text for cells
@property (strong, nonatomic) NSMutableArray *data;

// the number of rows to show
@property (nonatomic) NSInteger numberOfRowsToShow;

// when toolbar buttons are pressed
- (void)showModalViewButtonPressed;
- (void)redrawVisibleCellsButtonPressed;
- (void)insertButtonPressed;
- (void)deleteButtonPressed;

@end

@implementation MyViewController

@synthesize mainTableView;
@synthesize mainToolbar;
@synthesize data;

@synthesize numberOfRowsToShow;

- (void)viewDidLoad {
    [super viewDidLoad];
    // create data array for cell contents
    self.data = [[NSMutableArray alloc] initWithObjects:
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 @"سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام سلام",
                 nil];
    self.numberOfRowsToShow = 0;
    // set toolbar buttons
    [self.mainToolbar setItems:[NSArray arrayWithObjects:
                                [[UIBarButtonItem alloc] initWithTitle:@"Show Modal View" style:UIBarButtonItemStyleBordered target:self action:@selector(showModalViewButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Redraw Visible Cells" style:UIBarButtonItemStyleBordered target:self action:@selector(redrawVisibleCellsButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Insert" style:UIBarButtonItemStyleBordered target:self action:@selector(insertButtonPressed)],
                                [[UIBarButtonItem alloc] initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(deleteButtonPressed)],
                                nil] animated:YES];
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // clean up
    self.data = nil;
    self.mainTableView = nil;
    self.mainToolbar = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // only one section
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // return the number of items in the data array
    return self.numberOfRowsToShow;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // dequeue cell from storyboard
    UITableViewCell *cell = [self.mainTableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
    // get text view
    UITextView *cellText = (UITextView *)[cell viewWithTag:1];
    // set text using data array
    cellText.text = [self.data objectAtIndex:indexPath.row];
    // set large font
    cellText.font = [UIFont systemFontOfSize:20.0f];
    // set background color based on whether the line spacing is correct
    if (cellText.contentSize.height == 100.0f) {
        // correct height sets a green background
        cellText.backgroundColor = [UIColor greenColor];
    } else if (cellText.contentSize.height == 88.0f) {
        // wrong height sets a red background
        cellText.backgroundColor = [UIColor redColor];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // return constant height
    return 150.0f;
}

- (void)textViewDidChange:(UITextView *)textView {
    // get parent cell for text view
    UITableViewCell *parentCell = (UITableViewCell *)textView.superview.superview;
    // get parent cell indent path
    NSIndexPath *parentIndexPath = [self.mainTableView indexPathForCell:parentCell];
    // save new text to data array
    [self.data replaceObjectAtIndex:parentIndexPath.row withObject:textView.text];
}

#pragma mark - Other methods

- (void)showModalViewButtonPressed {
    // segue to modal view
    [self performSegueWithIdentifier:@"modalSegue" sender:self];
}

- (void)redrawVisibleCellsButtonPressed {
    // reload all visible cells in the table view
    [self.mainTableView reloadRowsAtIndexPaths:[self.mainTableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)insertButtonPressed {
    // insert a cell in the table view (increments number of rows to show first)
    [self.mainTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:self.numberOfRowsToShow++ inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)deleteButtonPressed {
    // delete a cell in the table view (decrements number of rows to show first)
    [self.mainTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:--self.numberOfRowsToShow inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

@end
#import <UIKit/UIKit.h>

@interface MyModalViewController : UIViewController

- (IBAction)closeButtonPressed:(id)sender;

@end
#import "MyModalViewController.h"

@implementation MyModalViewController

- (IBAction)closeButtonPressed:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

@end
(1)运行应用程序。当屏幕加载时,UITableView将没有内容

(2)轻触工具栏中的“插入”按钮几次。两个单元格将以绿色添加。请注意,对于仅包含阿拉伯语的行,此处的行距比仅包含英语的行高4像素(字体大小为20)(您可以在单元格中的所有英语文本中自己尝试)。在此阶段,您将看到应用程序的正确行为

(3)点击“显示模式视图”按钮,当模式视图打开时,点击UITextField内部以打开键盘。点击“完成”关闭模式视图。从这一点开始,错误将在UITableView中发生(请参阅下一步)

(4)再点击几次“插入”。请注意,所有新的单元格都是红色的。这是因为新单元格中的行间距是错误的——它们是文本全部为英文字母时使用的间距,因为不再像以前那样为阿拉伯语添加额外的4个像素

(5)要真正看到一些bug,请点击“重画可见单元格”按钮,并在重画单元格时观察UITableView周围的颜色变化


讨论:似乎在模式视图中打开键盘后,新的单元格出列时,阿拉伯语和其他非英语文本的行高错误。这似乎与UITableView的缓存机制有关。挑战在于找到一种解决方案,在任何情况下都能加载适当的行距。

这不是一个完美的解决方案,而是一种解决方法。真正的解决方案将在苹果修复这个bug时出现

请注意:此答案由苹果开发者支持部推荐

当UITextField或UISearchBar成为带有样式表的模式视图中的第一个响应程序后,UITableViewCells内的所有UITextView中都会出现此错误

解决方法是:

  • 子类UITextView创建自己版本的UITextField和UISearchBar,而不是使用实际控件
  • 不要在UITableViewCell内使用UITextView。相反,使用核心文本创建自己的版本

  • 选项1速度快得多,但在滚动方面也有自己的挑战。选项2运行良好,但需要很长时间。研究EGOTextView,寻找一个好的起点。

    请将此错误提交给苹果:我已将其作为错误ID#11961309提交,但即使苹果确实解决了这一问题,仍然需要一个解决方案来支持iOS 5.x的向后兼容应用程序。也许可以将示例项目放在人们可以下载的地方。期望有人按照所有步骤重新创建您的项目是获得帮助的一个障碍!好点子@idz!您可以在下载该项目。我还更新了上面的描述,让用户知道。