【问题标题】:UITableView accessoryType disappears in last half of UITableViewUITableView accessoryType 在 UITableView 的后半部分消失
【发布时间】:2013-07-17 15:10:29
【问题描述】:

我有一个 iPad 应用程序(XCode 4.6、ARC、Storyboards、iOS 6.2.3)。我有一个带有 21 行的 UITableView 的 UIPopover。我可以在所有行中随机设置 accessoryType,但只有在前 12 行中,accessoryType 设置(复选标记)才会持续存在,因此可以用另一种方法检查它处理。我看不出前 12 行和后 9 行之间有任何区别。 UITableView 是可滚动的,所以要到达第 11 行之后的行,你必须滚动到底部

这里是设置accessoryType的代码:

#pragma mark didSelectRowAtIndexPath

- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath {

    //  get the cell that was selected
    UITableViewCell *theCell = [tableView cellForRowAtIndexPath:indexPath];

    if(theCell.accessoryType != UITableViewCellAccessoryCheckmark)
        theCell.accessoryType = UITableViewCellAccessoryCheckmark;
    else
        theCell.accessoryType = UITableViewCellAccessoryNone;
}

这是我检查 accessoryType 并对其进行处理的代码:

-(void) moveServices  {  //  (moves checked tableViewRows to services tableview)

NSMutableString *result = [NSMutableString string];

for (int i = 0; i < [servicesArray count]; i++) {
    NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:0];
    [tvServices scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

    UITableViewCell *cell = [tvServices cellForRowAtIndexPath:path];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        [result appendFormat:@"%@, ",cell.textLabel.text];
    NSLog(@"\n\ni: %d\ncell.accessoryType: %d\ncell.textLabel: %@",i,cell.accessoryType, cell.textLabel);
    }
}

if (result.length > 2) {  //  move to text box in main menu
    storeServices =[result substringToIndex:[result length] - 2];
}

}

【问题讨论】:

    标签: objective-c uitableview ios6


    【解决方案1】:

    您似乎将“数据源”的概念与表格中单元格的内容混为一谈。不要那样做 - 将您的数据源(表中的特定行是否应根据您的程序逻辑显示复选标记)与特定单元格的设置(特定单元格是否显示复选标记)分开。然后在cellForRowAtIndexPath 中,构建单元格以匹配数据源的当前设置。原因是 UITableView 会根据屏幕上可见的行重用单元格实例(这只是很好的 MVC 设计)。

    在您的情况下,您应该在您的类中保留一个 NSMutableArray 属性,该属性记录整个表的设置,并使用 cellForRowAtIndexPath 中该数组的值来设置该特定单元格。然后控制器中的其他“业务逻辑”方法使用数组属性来查询模型状态,而不是单元格设置(它们是视图的一部分,应该独立于数据模型)。​​

    【讨论】:

    • 那为什么它对前 12 行有效,而不是其余行?
    • 它只能靠运气“工作”。因为这 12 行是(当前)可见的,因此 UITableView 为它们创建了单元格。一旦向下滚动足够远,执行该“工作”的行范围就会改变。这就是为什么你不这样做。将数据源与视图分开,这就是 UITableView 的设计方式,遵循它,一切都会好起来的。
    • 好的...我完全理解您在说什么...但是,我不知道我必须在代码中进行哪些更改以适应您的建议。你能给我举个例子或文档吗?
    • 我找到了this,它准确地解释了我必须做什么...我需要保留一个包含每行状态的数组...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多