首页 > 解决方案 > 如何在不重新加载Objective-C中的行的情况下更改行中的特定元素?

问题描述

我想UiSwitchUITableView. 我想以编程方式打开和关闭动画。

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

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


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

标签: iosobjective-cuiswitch

解决方案


问题在于您如何检索cell. 它应该是:

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

不是这个:

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

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


推荐阅读