1

我创建了一个可编辑的 UITableView,它支持我自己设计的几个自定义 UITableViewCell。每当用户创建新记录或修改现有记录时,他们都会使用此 UITableView。我使用分组样式表视图将表视图分成几组 tableViewCells。

当用户正在编辑记录时,我不希望显示第 1 部分。为了实现这一点,我在调用 numberOfRowsInSection 方法时返回 0。一切正常,但是第 0 部分和第 2 部分之间存在“轻微的视觉差距”,如果可能的话,我想消除它。我想避免重新编码表视图控制器来动态处理 indexPaths。我的许多 indexPaths(部分和行)都是硬编码的。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

printf("CustomEntryViewController, -tableView:numberOfRowsInSection:\n");

if (tableView == self.customEntryTableView) {

    if (section == 0) 

        return 1;

    else if (section == 1) {

        if ([self.entryMode isEqualToString:@"ADD"]) 
            return 2;
        else if ([self.entryMode isEqualToString:@"EDIT"])
            return 0;

    }
    else if (section == 2)

        return 1;

    else if (section == 3)

        return 1;

    else if (section == 4 && self.uisegQuantityAnswer.selectedSegmentIndex == 0) 

        return 1;

}

return 0;
}

提前致谢。

4

1 回答 1

1

我找到了解决问题的方法。我在视图控制器中创建了两个辅助方法来虚拟化由 tableView 委托传递给我的部分。每当我打算禁用表格视图中的一个部分时,虚拟部分会导致我编写的逻辑被跳过。

- (NSIndexPath *)virtualIndexPath:(NSIndexPath *)indexPath {

    return [NSIndexPath indexPathForRow:indexPath.row inSection:[self virtualSection:indexPath.section]]; 

}


- (NSUInteger)virtualSection:(NSUInteger)section {

    NSUInteger virtualSection;

    if ([self.entryMode isEqualToString:@"ADD"])

        virtualSection = section;

    else if ([self.entryMode isEqualToString:@"EDIT"]) {

        if (section == 0) 
            virtualSection = section;

        else if (section > 0)
            virtualSection = section + 1;

    }

    return virtualSection;

}

然后我在整个应用程序的其余部分调用上述方法之一。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSUInteger virtualSection = [self virtualSection:section];

    if (tableView == self.customEntryTableView) {

        if (virtualSection == 0) {

// Additional code....

除此之外,我还修改了 -tableView:numberOfSectionsInTableView: 方法以返回每个 ADD 与 EDIT 模式的正确节数。

由于这只是一种解决方法,我将把这个问题标记为未回答。

于 2011-04-25T12:28:17.423 回答