2012-04-19 54 views
2

我已经使用cellForRowAtIndexPath中的以下代码设置了自定义分组tableview样式。每个单元格都有一个新的背景视图,角落根据其在该部分中的位置进行四舍五入。我在整个应用程序和各种视图中使用这种风格,我使用动画添加和删除行。 (使用[tableview insertRowsAtIndexPaths:]等)。自定义UITableViewCell的背景,何时重新加载等

如果某一节的最后一行被插入或删除,我需要能够重新加载单元格背景视图角。我怎么能做到这一点,而无需调用[tableview reloadData]甚至reloadCellsAtIndexPaths?这两个函数都会混淆插入和删除单元格的动画。有什么地方我可以把这个背景角落定义在适当的时候调用?默认的分组tableview如何做到这一点?

对不起,如果这不清楚。要求澄清,我会编辑。谢谢! :)

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]; 
     } 
    } 
+0

会有一个干净的方式来检测单元layoutSubviews或drawRect应该绘制哪些角?我不介意子类UITableViewCell,我只是想不出一种方式,它会帮助.. – Dash 2012-04-19 00:26:56

回答

0

如果您有要求的自定义动画,在动画完成,或者说之前的新小区的显示,使用的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; 
     } 
} 

希望这有助于!

0

我认为这是你要找的。在您更改模型后,请致电:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation 

这将导致您的cellAtIndexPath方法被调用。发送一个具有过时行的单个索引路径的数组(我猜对于圆角问题,这是行被删除后的最后一行,或者是新添加的最后一行之前的行)。

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

0

为我工作的解决方案是这样的:

  1. 坐落在的tableView背景:willDisplayCell:forRowAtIndexPath:
  2. 创建一个方便的方法叫reloadBackgroundForRowAtIndexPath调用willDisplayCell手动
  3. 当我添加行,在延迟之后调用便捷方法以允许表格动画完成

    - (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)]; 
    
相关问题