我一直在努力解决这个问题,但我似乎找不到一种优雅的方式。我试图在创建单元格后注入代码以使UITextView
我的自定义成为第一响应者。UITableViewCell
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
// store some of the cell information
_newlyCreatedCell = @{@"path": newIndexPath, @"age":[NSDate date],@"cell":(CustomCell*)[tableView cellForRowAtIndexPath:newIndexPath]};
[[(CustomCell*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:newIndexPath.row-1 inSection:0]] textLabel] becomeFirstResponder];
// highlight cell
[tableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
[tableView deselectRowAtIndexPath:newIndexPath animated:YES];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
我得到了这个粗略的代码,但它似乎不是最好的方法或有效的方法。我有一个粗略而错误的想法,将新创建的单元格的信息存储在NSDictionary
. 很坏。无论如何,然后我开始摆弄更改newIndexPath
值,因为它没有返回NSIndexPath
新创建的单元格(可能写在某处的Apple文档中......)。所以我将值设置为[NSIndexPath indexPathForRow:newIndexPath.row-1 inSection:0]
,当然期望它会返回正确的值。但这又回来了(null)
。没啥事儿。
有人知道在创建新单元格时执行一堆代码的更好且可能更简单的方法吗?