我有一个名为 ReminderCell
的 UITableViewCell
子类。
ReminderCell
有一个名为 backToPlaceAnimation
的方法,是我编写的。
例如,如果我想使用 RemindersVC
在第一个 ReminderCell
上调用 backToPlaceAnimation
,使用 tableView: cellForRowAtIndexPath:
它没有返回正确的对象(它是 UITableViewCell
对象,根本不是 ReminderCell
)。
如何获得正确的对象?
代码:
NSIndexPath *cellIndexPath=[NSIndexPath indexPathWithIndex:0];
ReminderCell *cell=(ReminderCell*)[self tableView:self.AroundersTableView cellForRowAtIndexPath:cellIndexPath];
[cell cancelDeletion];
崩溃日志:
*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“用于 UITableView 的索引路径无效。传递给 TableView 的索引路径必须恰好包含两个指定部分和行的索引。如果可能,请在 UITableView.h 中使用 NSIndexPath 上的类别。
cellForRowAtIndexPath:覆盖:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Creating a default cell using "cellIdentifier"(NSString)
ReminderCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell==nil) {
cell=[[ReminderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
//Changing "cell"'s "index" to indexPath.row
cell.index=(int)indexPath.row; //Crashes here
cell.textLabel.text=@"Hello";
//Returning cell
return cell;
}
谢谢!
最佳答案
您应该将 [NSIndexPath indexPathWithIndex:0];
替换为 [NSIndexPath indexPathWithIndex:0 inSection:0];
关于ios - 返回 UITableViewCell 自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35723711/