Iphone 自定义UITableViewCell背景、何时重新加载等

Iphone 自定义UITableViewCell背景、何时重新加载等,iphone,objective-c,ios,uitableview,Iphone,Objective C,Ios,Uitableview,我在CellForRowatineXpath中使用以下代码设置了自定义分组表视图样式。每个单元都会被赋予一个新的背景视图,并且其角会根据其在剖面中的位置进行圆角处理。我在整个应用程序中使用这种样式,在各种视图中,我使用动画添加和删除行。使用[tableview insertRowsAtIndexPaths:]等 如果插入或删除节的最后一行,我需要能够重新加载单元格背景视图角。我如何在不调用[tableview reloadData]甚至不调用ReloadCellSatinDexpath的情况下实

我在CellForRowatineXpath中使用以下代码设置了自定义分组表视图样式。每个单元都会被赋予一个新的背景视图,并且其角会根据其在剖面中的位置进行圆角处理。我在整个应用程序中使用这种样式,在各种视图中,我使用动画添加和删除行。使用[tableview insertRowsAtIndexPaths:]等

如果插入或删除节的最后一行,我需要能够重新加载单元格背景视图角。我如何在不调用[tableview reloadData]甚至不调用ReloadCellSatinDexpath的情况下实现这一点?这两个函数都会搞乱插入和删除单元格的动画。有什么地方我可以把这个背景角定义放在适当的时候调用吗?默认分组表视图是如何做到这一点的

如果不清楚,很抱歉。要求澄清,我会编辑。谢谢

UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:[self styleForCellAtIndexPath:indexPath] 
                                       reuseIdentifier:cellIdentifier] autorelease];

        // Theme the cell
            TableCellBackgroundView *backgroundView = [[TableCellBackgroundView alloc] initWithFrame:cell.frame];
            TableCellBackgroundView *selectedBackgroundView = [[TableCellBackgroundView alloc] initWithFrame:cell.frame];
            selectedBackgroundView.selectedStyle = YES;
            // top row, without header
            BOOL header = [self tableView:aTableView heightForHeaderInSection:indexPath.section]!=0;
            if (indexPath.row == 0 && (!header || tableTheme==kTableThemeSimple)) {
                backgroundView.topRadius = 6;
                selectedBackgroundView.topRadius = 6;
            }
            // bottom row
            if (indexPath.row == [self tableView:aTableView numberOfRowsInSection:indexPath.section]-1) {
                backgroundView.bottomRadius = 6;
                selectedBackgroundView.bottomRadius = 6;
            }
            cell.backgroundView = backgroundView;
            cell.selectedBackgroundView = selectedBackgroundView;
            cell.contentView.backgroundColor = [UIColor clearColor];
            cell.backgroundColor = [UIColor clearColor];
        }
    }

如果有为自定义动画调用的方法,则在动画完成时,或者在显示新单元格之前,使用UITableViewCell的子类调用名为-void fixCornersWithIndexPath:NSIndexPath*indexPath的方法

- (void) customAnimationForCellAtIndexPath: (NSIndexPath *) path {
    //your animation things
    CustomCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];
    [cell fixCornersWithIndexPath: path andRowCount: [self tableView:aTableView numberOfRowsInSection:indexPath.section]];
}
然后修复角点应该是这样的:

- (void) fixCornersWithIndexPath: (NSIndexPath *) indexPath andRowCount: (NSInteger *) rowCount {
      if (indexPath.row == 0 && (!header || tableTheme==kTableThemeSimple)) {
            self.backgroundView.topRadius = 6;
            self.selectedBackgroundView.topRadius = 6;
        }
        // bottom row
        if (indexPath.row == rowCount-1) {
            self.backgroundView.bottomRadius = 6;
            self.selectedBackgroundView.bottomRadius = 6;
        }
}

希望这有帮助

如果您有一个为自定义动画调用的方法,则在动画完成时,或者更确切地说在显示新单元格之前,使用UITableViewCell的子类调用名为-void fixCornersWithIndexPath:NSIndexPath*indexPath的方法

- (void) customAnimationForCellAtIndexPath: (NSIndexPath *) path {
    //your animation things
    CustomCell *cell = [self.tableView cellForRowAtIndexPath: indexPath];
    [cell fixCornersWithIndexPath: path andRowCount: [self tableView:aTableView numberOfRowsInSection:indexPath.section]];
}
然后修复角点应该是这样的:

- (void) fixCornersWithIndexPath: (NSIndexPath *) indexPath andRowCount: (NSInteger *) rowCount {
      if (indexPath.row == 0 && (!header || tableTheme==kTableThemeSimple)) {
            self.backgroundView.topRadius = 6;
            self.selectedBackgroundView.topRadius = 6;
        }
        // bottom row
        if (indexPath.row == rowCount-1) {
            self.backgroundView.bottomRadius = 6;
            self.selectedBackgroundView.bottomRadius = 6;
        }
}

希望这有帮助

我想这就是你要找的。更改模型后,请致电:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
这将导致调用cellAtIndexPath方法。发送一个数组,其中包含一个过期的行的索引路径。我想对于圆角问题,这是删除行之后的最后一行,或者是新添加的最后一行之前的行


在开始/结束更新之间,您可以将其与其他更新方法一起调用。

我想这就是您要寻找的。更改模型后,请致电:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
这将导致调用cellAtIndexPath方法。发送一个数组,其中包含一个过期的行的索引路径。我想对于圆角问题,这是删除行之后的最后一行,或者是新添加的最后一行之前的行


在开始/结束更新之间,您可以将其与其他更新方法一起调用。

对我有效的解决方案是:

在tableView:willDisplayCell:forRowAtIndexPath: 创建一个名为reloadbackgroundforrowatinexpath的方便方法,手动调用willDisplayCell 当我添加一行时,在延迟后调用便利方法,以允许表动画完成

- (void)reloadBackgroundForRowAtIndexPath:(NSIndexPath*)indexPathToReload {
    [self tableView:self.tableView willDisplayCell:[self.tableView cellForRowAtIndexPath:indexPathToReload] forRowAtIndexPath:indexPathToReload];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // Logic to determine the background image
}

// elsewhere, update the table, then
NSIndexPath *indexPathToFix = [NSIndexPath indexPathForRow:count-1 inSection:0];
// If the new row is coming out, we want to update the background right away.  If the new row is going away, we want to wait to update until the animation is done.
// I don't like hard-coding the animation length here, but I don't think I have a choice.
[self performSelector:@selector(reloadBackgroundForRowAtIndexPath:)  withObject:indexPathToFix afterDelay:(self.tableView.isEditing ? 0.0 : 0.2)];

对我有效的解决方案是:

在tableView:willDisplayCell:forRowAtIndexPath: 创建一个名为reloadbackgroundforrowatinexpath的方便方法,手动调用willDisplayCell 当我添加一行时,在延迟后调用便利方法,以允许表动画完成

- (void)reloadBackgroundForRowAtIndexPath:(NSIndexPath*)indexPathToReload {
    [self tableView:self.tableView willDisplayCell:[self.tableView cellForRowAtIndexPath:indexPathToReload] forRowAtIndexPath:indexPathToReload];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // Logic to determine the background image
}

// elsewhere, update the table, then
NSIndexPath *indexPathToFix = [NSIndexPath indexPathForRow:count-1 inSection:0];
// If the new row is coming out, we want to update the background right away.  If the new row is going away, we want to wait to update until the animation is done.
// I don't like hard-coding the animation length here, but I don't think I have a choice.
[self performSelector:@selector(reloadBackgroundForRowAtIndexPath:)  withObject:indexPathToFix afterDelay:(self.tableView.isEditing ? 0.0 : 0.2)];

是否有一种干净的方法来检测单元格布局子视图或drawRect中应该绘制哪些角?我不介意将UITableViewCell子类化,我只是想不出有什么方法可以帮上忙..有没有一种干净的方法可以在cells LayoutSubView或drawRect中检测应该绘制哪些角点?我不介意将UITableViewCell子类化,我只是想不出有什么办法能帮上忙。。