代码之家  ›  专栏  ›  技术社区  ›  FS.O

返回UITableViewCell自定义单元格

  •  1
  • FS.O  · 技术社区  · 8 年前

    我有一个子类 UITableViewCell 打电话 ReminderCell .

    提醒单元格 有一个名为 backToPlaceAnimation 我写的。

    例如,问题是如果我想打电话 backToPlaceAnimation 在第一天 提醒单元格 从…起 RemindersVC 使用 tableView: cellForRowAtIndexPath: 它没有返回正确的对象(它是 UI表格视图单元格 对象而不是 提醒单元格 完全)。

    我怎样才能得到正确的对象?

    代码:

    NSIndexPath *cellIndexPath=[NSIndexPath indexPathWithIndex:0];
    
    ReminderCell *cell=(ReminderCell*)[self tableView:self.AroundersTableView cellForRowAtIndexPath:cellIndexPath];
    
    [cell cancelDeletion];
    

    崩溃日志:

    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.

    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;
    }
    

    非常感谢。

    5 回复  |  直到 8 年前
        1
  •  1
  •   Dante Puglisi    8 年前

    你应该更换 [NSIndexPath indexPathWithIndex:0]; 具有 [NSIndexPath indexPathWithIndex:0 inSection:0];

        2
  •  0
  •   Dante Puglisi    8 年前

    您正在声明 cell UITableViewCell ,声明为 ReminderCell 如果你希望它是那种类型

        3
  •  0
  •   gvuksic    8 年前

    将单元格定义为ReminderCell

    ReminderCell *cell = [self.AroundersTableView cellForRowAtIndexPath:cellIndexPath];
    
        4
  •  0
  •   Black Magic    8 年前

    CellForRowAtIndexPath应返回ReminderCell而不是UITableViewCell

    因此,应该从以下内容开始:

    -(ReminderCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    
        5
  •  0
  •   Steve    8 年前

    根据上面显示的错误,您无法从逐行单元格方法中获取单元格,因为您创建的索引路径不正确。尝试使用 [NSIndexPath indexPathForRow:0 inSection:0] (注意:您需要更改0以反映所需的单元格。