Core data 使用NSFetchedResultsController实现自定义表视图单元格(带有标签和自定义大小)
我正在开发一个简单的应用程序,它有以下前提:Core data 使用NSFetchedResultsController实现自定义表视图单元格(带有标签和自定义大小),core-data,uitableview,nsfetchedresultscontroller,custom-cell,Core Data,Uitableview,Nsfetchedresultscontroller,Custom Cell,我正在开发一个简单的应用程序,它有以下前提: 带有加号按钮的TableViewController-用户按下按钮,并以模式显示要输入的文本字段,以表示名称、标题等。当用户按下save时,视图控制器将消失,并将这些条目添加到核心数据数据库中,条目将显示在表视图中 我可以很好地使用标准单元格,如subtitle等。我正在使用NSFetchedResultsController,现在我想部署自定义单元格 我将样式更改为故事板中的自定义单元格,并添加了三个标签,表示名称、标题和金额(由用户添加) 我
- 带有加号按钮的TableViewController-用户按下按钮,并以模式显示要输入的文本字段,以表示名称、标题等。当用户按下save时,视图控制器将消失,并将这些条目添加到核心数据数据库中,条目将显示在表视图中李>
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"TableCellID";
TableCell *tablecell = (TableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSDictionary * dict = [self.tweetsArray objectAtIndex:indexPath.row];
tablecell.title.text = [dict objectForKey:@"tweet"];
UIImage *imageIcon = [UIImage imageNamed:[dict objectForKey:@"image"]];
[tablecell.cellImage setImage:imageIcon];
return tablecell;
}
这不是我的代码,而是引用自:但概念是相同的
因为我使用的是NSFetchedResultsController,所以我实际上有两种方法用于单元格:
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Transaction *transaction = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.nameLabel.text =[NSString stringWithFormat:@"%@", transaction.name];
cell.amountLabel.text = [NSString stringWithFormat:@"%@", transaction.amount];
cell.eventLabel.text = [NSString stringWithFormat:@"%@", transaction.title];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"People";
TransactionCell *cell = (TransactionCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
此代码不起作用,因为实际上无法识别nameLabel、amountLabel
我已经(像上面的代码一样)将所有信息放入cellForRowAtIndexPath中,但随后从
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
方法,因为它具有以下条目:
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
我已尝试更改的方法签名
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
将是:
- (void)configureCell:(TransactionCell *)cell atIndexPath:(NSIndexPath *)indexPath {
但这在我看来并不正确,而且还有一些警告
所以基本上我不想做任何复杂的事情。我只是尝试创建一个带有三个标签的自定义单元格-为什么在configureCell方法中无法识别CellForRowatineXpath中的“单元格”
最后,自定义单元格有一个特定的大小,比普通单元格大,并且在应用程序运行时不会出现;情况仍然正常。我在单元格的inspector中这样做了,但也在table view类的末尾添加了以下代码:
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
tableView.rowHeight = 80;
}
但它仍然保持正常高度
在此方面的任何帮助都将不胜感激
谢谢,您有两个选择:
- 将
的签名更改为configureCell
并添加一个附加的强制转换- (void)configureCell:(TransactionCell *)cell atIndexPath:(NSIndexPath *)indexPath
case NSFetchedResultsChangeUpdate: [self configureCell:(TransactionCell *)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break;
- 或者将
的签名保持原样,并在方法内部强制转换:configureCell
(请注意,所有- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { TransactionCell *tcell = (TransactionCell *)cell; Transaction *transaction = [self.fetchedResultsController objectAtIndexPath:indexPath]; tcell.nameLabel.text = transaction.name; tcell.amountLabel.text = transaction.amount; tcell.eventLabel.text = transaction.title; }
调用都是不必要的。)stringWithFormat:
我会使用第二种方法,但这只是口味的问题。这是因为你可能忘记打电话了
self.tableView注册表类:强制重用标识符:
或
self.tableView注册表nb:forCellReuseIdentifier:
在视图中加载(假设self.tableView指向UITableView)
顺便说一句,不要两者都执行!如果为单元格制作了xib,请单独使用registerNib
。如果您创建了UITableViewCell子类,请单独使用registerClass
。请注意,如果单元在情节提要中定义为“原型单元”,那么您根本不必注册它(而且您不应该注册)。好吧,我不知道。但XIB有时可能不稳定,这值得一试。谢谢尼古拉斯和马丁——这确实是有道理的,但我觉得使用马丁在下面的回答更舒服,因为这对我很有用。非常感谢你的建议,非常感谢!再次感谢马林。第二种方法很有效。我不喜欢更改方法签名(因为我不知道它会产生什么样的效果,等等),但是第二种方法非常有效。非常感谢。你知道牢房的大小吗?我在inspector中设置了它,甚至添加了该方法,但它总是默认为单元格的原始大小,我不完全确定为什么@amitsbajaj:不客气你有搜索显示控制器吗?否则您可以只设置tableView.rowHeight=80代码>在viewDidLoad
中。如果这没有帮助,那么也许可以开始一个新的问题,因为这是一个单独的问题。谢谢马丁-这是这里的关键,它正在发挥作用。它的显示有点奇怪,但我会提出另一个问题。再一次,在你的帮助下,我正在进步我的学习(和我的应用程序),所以我很感激!