【问题标题】:how to change UITableViewCell from outside tableview methodes如何从外部 tableview 方法更改 UITableViewCell
【发布时间】:2013-03-27 14:16:22
【问题描述】:

我有一个 UITableView,它持有一个 UISwitch 作为 UITableViewCell 附件视图。

我正在尝试创建一种响应开关更改的方法。 基本上在任何给定时间都应该只有一个阀门打开,所以我希望当我打开一个开关时,它会使已经打开的开关关闭。

我做了类似的事情:

-(IBAction)openCloseValve:(id)sender

{

UISwitch *theSwitch = (UISwitch *)sender;

if ([theSwitch isOn])

{

    //Open Valve!

    NSLog(@"Opening Valve!");

    Storage *strg = [Storage sharedStorage];

    

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:strg.openValve.intValue-1 inSection:0]];

    UISwitch *newSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];

    newSwitch.onTintColor = [UIColor greenColor];

    newSwitch.on = NO;

    oldCell.accessoryView = newSwitch;

    
    strg.openValve= [[NSNumber alloc] initWithInt:theSwitch.tag + 1];

    NSLog(@"%d", strg.openValve.intValue);

} else {

    //Close Valve!

    NSLog(@"Closing Valve!");

}

}

到目前为止,它什么也没做。 我知道正在调用该方法。 我做错了什么?

【问题讨论】:

    标签: uitableview uiswitch


    【解决方案1】:

    旧单元格需要通过 [tableView reloadCellsAtIndexPath:] 进行更新。您可以通过更新旧开关而不是创建新开关来避免这种情况:

    UISwitch *oldSwitch = (UISwitch *)oldCell.accessoryView;
    oldSwitch.on = NO;
    

    【讨论】:

    • 我已经尝试过了,但它不起作用。我认为 oldCell 并没有真正得到原始单元格,因为我尝试 NSLog 单元格的 textLabel 以准确验证这一点,但我得到了 null。我一定是错过了什么。
    • 您传递给 cellForRowAtIndexPath 方法的索引路径似乎有问题。
    • 请您 NSLog [NSIndexPath indexPathForRow:strg.openValve.intValue-1 inSection:0] 的值并确保它是正确的吗?顺便说一句,你有没有想过使用免费的 Sensible TableView 框架?我相信它使执行诸如此类的任务变得非常简单。我已经使用它一段时间了,我无法想象再次回到手动操作。
    • 2 个索引 [0, 3]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多