如何更改行中的特定元素而不重新加载 Objective-C 中的行?

How to change specific element in row without reloading row in Objective-C?

我想在 UITableView 中连续更新一个 UiSwitch。我想以编程方式使用 animation 打开和关闭它。

    NSIndexPath *indexPath = [[NSIndexPath indexPathForRow:0 inSection:lastSelectedCardSectionNumber] retain];
    newCell *cell = (newCell *)[self.myTableView dequeueReusableCellWithIdentifier:@"newCell" forIndexPath:indexPath];
    [cell.switch1 setOn:YES animated:YES];

但上面的行没有改变状态,我必须重新加载行,这不会使 UiSwitch 状态随动画改变。


开关顶部有一个视图,用户可以点击它来更改开关的状态。但它应该在某些情况下改变。因此我不允许用户更改它我想以编程方式更改它。

问题在于您检索 cell 的方式。应该是:

newCell *cell = (newCell *)[self.tableView cellForRowAtIndexPath:indexPath];

不是这个:

newCell *cell = (newCell *)[self.myTableView dequeueReusableCellWithIdentifier:@"newCell" forIndexPath:indexPath];

前者获取已经存在的单元格实例,而后者创建单元格的新实例。