Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios cellForRowAtIndexPath调用_Ios_Uitableview - Fatal编程技术网

Ios cellForRowAtIndexPath调用

Ios cellForRowAtIndexPath调用,ios,uitableview,Ios,Uitableview,我正在开发一个使用表视图的ToDoList iOS应用程序。单元格内容(UITableCellView)由nsmutablearray的实例变量填充 当我添加一个新的ToDoItem时,我添加的每一行都会显示两次内容。当我放置断点并调试代码时,我发现我添加的每一行都会调用两次CellForRowatineXpath方法。为什么会这样。如何处理这种情况,或者是否有其他方法返回UITableCellView单元格进行显示 方法如下所示 - (UITableViewCell *)tableView:(

我正在开发一个使用表视图的ToDoList iOS应用程序。单元格内容(UITableCellView)由nsmutablearray的实例变量填充

当我添加一个新的ToDoItem时,我添加的每一行都会显示两次内容。当我放置断点并调试代码时,我发现我添加的每一行都会调用两次CellForRowatineXpath方法。为什么会这样。如何处理这种情况,或者是否有其他方法返回UITableCellView单元格进行显示

方法如下所示

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell;
    cell = [self.tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ListPrototypeCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    ToDoItem *item = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = item.itemName;
    return cell;
}
当用户添加新项时调用此方法

-(IBAction)unwindToList:(UIStoryboardSegue *)segue{

    AddToDoItemViewController *ITEM = [segue sourceViewController];
    ToDoItem *tItem = ITEM.toDoItem;
    if (tItem!=nil) {
        [self.toDoItems addObject:tItem];
        [self.tableView reloadData];
        [self setItems:self.toDoItems];
    }  
}
编辑:

tDItems只是我正在使用的另一个实例变量
提前感谢

如果阵列中添加了重复的项目,请检查您的阵列,因为即使两次调用CellForRowatineXpath,如果阵列中的项目不重复,也不会创建重复的单元格。cellForRowAtIndexPath可能会因为多次重新加载而被调用两次

我认为问题在于重新加载表数据..尝试像这样重新加载数据…可能有效吗

-(IBAction)unwindToList:(UIStoryboardSegue *)segue{

    AddToDoItemViewController *ITEM = [segue sourceViewController];
    ToDoItem *tItem = ITEM.toDoItem;
    if (tItem!=nil) {
        [self.toDoItems addObject:tItem];
        [self setItems:self.toDoItems];
    }
    [self.tableView reloadData];  
}

使用
insertRowsAtIndexPaths
[ToDoItem addObject:[NSIndexPath IndeExpathForRow:i Instition:0]];
其中i=indepath表示要插入元素的位置

`[tableView开始更新]

      [insertRowAtIndexArray removeAllObjects];

      [insertRowAtIndexArray addObject:[NSIndexPath indexPathForRow:i inSection:0]];
      [toDoItems addObject:[NSIndexPath indexPathForRow:i inSection:0]];

      i++;


[tableView insertRowsAtIndexPaths:insertRowAtIndexArray withRowAnimation:(UITableViewRowAnimationAutomatic)];

[tableView endUpdates];`

我做了一个小修理,它工作了。下面是代码

 -(IBAction)unwindToList:(UIStoryboardSegue *)segue{

AddToDoItemViewController *ITEM = [segue sourceViewController];
ToDoItem *tItem = ITEM.toDoItem;
if (tItem!=nil) {

    if ([self.toDoItems count] == 0) {    // This is the fix made
       [self.toDoItems addObject:tItem];
    }

    [self.tableView reloadData];
    [self setItems:self.toDoItems];

}
}


但我不确定从第二个项目开始如何添加项目。addObject仅适用于第一个项目,并且视图中没有重复项。但是,如果我去掉那个if条件,那么可以看到重复的。现在我正在调试,看看为什么只有它对第一项有效。

用一些相关的代码更新您的问题。请查找代码您为NumberOfRowsInSection返回了什么我返回1。对于numberOfRowsInSection,我返回return[self.toDoItems count],我想知道setItems的作用是什么。我的数组中没有重复项
 -(IBAction)unwindToList:(UIStoryboardSegue *)segue{

AddToDoItemViewController *ITEM = [segue sourceViewController];
ToDoItem *tItem = ITEM.toDoItem;
if (tItem!=nil) {

    if ([self.toDoItems count] == 0) {    // This is the fix made
       [self.toDoItems addObject:tItem];
    }

    [self.tableView reloadData];
    [self setItems:self.toDoItems];

}