在动态tableView中重新加载包含textFields的单元格时的奇怪行为

时间:2015-04-29 14:22:17

标签: ios objective-c uitableview uitextfield

我有一个带有自定义单元格的动态tableView,每个单元格都包含一个textField和label。我想要以下行为:

用户点击“添加”按钮,表格在索引0处插入一个单元格,单元格的textField成为第一个响应者。

当用户完成输入数据时,单行textField将作为第一响应者辞职并隐藏自身。

然后单元格的标签变为可见,填充用户输入,并自动调整其高度以适应输入。此时单元格也会调整大小。

我有代码在第一次添加单元格时完成此操作,但是当添加更多单元格时,它会变得奇怪。第二个添加了细胞 s textField不会成为第一响应者,并且填充了前一个textField中的文本。随后添加的细胞显示为空白,除了每6个空白细胞外,第一个细胞的文本显示出来。

发生了什么,我该如何解决这个问题?

编辑:感谢@Rayfleck,我已经解决了第二个添加的单元格填充了前一个textField文本的问题,但是添加的第二个单元格(以及后续添加的单元格)仍然不是成为第一响应者。

以下是演示此行为的演示:

enter image description here

这是我的代码:

#define FONT_SIZE 16.0f
#define CELL_CONTENT_WIDTH 300.0f // This will change for different size!
#define CELL_CONTENT_MARGIN_WIDTH 10.0f
#define CELL_CONTENT_MARGIN_HEIGHT 20.0f

@interface ViewController ()

@property (strong) IBOutlet UITableView *tableView;
@property (strong) NSMutableArray *notesArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.notesArray = [NSMutableArray arrayWithObjects:@"Testo 1", @"This is a long string of text to show how the cell resizes to fit a variable height label.", @"Testo 3", @"Testo 4", @"Testo 5", nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (IBAction)addButtonTapped {

    NSMutableArray *indexPaths = [NSMutableArray array];
    [indexPaths addObject:[NSIndexPath indexPathForRow:0 inSection:0]]; // insert at first index

    [self.tableView setEditing:YES animated:NO]; // go into edit mode so cell has label hidden and textField visible

    // do the insertion
    [self.notesArray insertObject:@"" atIndex:0]; // placeholder for when user starts typing
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
    [self.tableView endUpdates];

    [self.tableView setEditing:NO animated:NO];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0]; // first table cell
    HC_NoteCell *cell = (HC_NoteCell *)[self.tableView cellForRowAtIndexPath:path];

    cell.noteTextField.hidden = YES;
    cell.noteLabel.text = cell.noteTextField.text;
    cell.noteLabel.hidden = NO;

    [self.notesArray replaceObjectAtIndex:0 withObject:cell.noteTextField.text];

    [textField resignFirstResponder];

    // Need to reload rows so cell height can be recalculated
    [self.tableView beginUpdates];
    [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];

    return YES;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.notesArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *text;
    text = [self.notesArray objectAtIndex:indexPath.row];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN_WIDTH * 2), 20000.0f);

    UIFont *font = [UIFont fontWithName:@"Helvetica Neue" size:FONT_SIZE];
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
                                                                forKey:NSFontAttributeName];
    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:text attributes:attrsDictionary];

    CGRect paragraphRect = [attributedString boundingRectWithSize:constraint options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil];

    CGFloat height = MAX(paragraphRect.size.height, 22.0f);

    return height + (CELL_CONTENT_MARGIN_HEIGHT * 2);

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NoteCell";

    HC_NoteCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[HC_NoteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (self.tableView.isEditing) {

        cell.noteLabel.hidden = YES;
        cell.noteTextField.hidden = NO;
        cell.noteTextField.delegate = self;

        [cell.noteTextField becomeFirstResponder];

    } else {

        cell.noteLabel.hidden = NO;
        cell.noteTextField.hidden = YES;
        cell.noteTextField.delegate = self;

        cell.noteLabel.text = [self.notesArray objectAtIndex:indexPath.row];
    }

    return cell;
}

@end

0 个答案:

没有答案