2013-11-09 72 views
1
- (UITableViewCell *)cellForInfoWithCellIdentifier:(NSString *)cellIdentifier 
             forIndexPath:(NSIndexPath *)indexPath 
             inTableView:(UITableView *) tableView 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    NSLog(@"%d", cell.contentView.subviews.count); 
    if (cell.contentView.subviews.count > 0) 
    { 
     [cell.contentView.subviews[0] removeFromSuperview]; 
    } 

    [cell.contentView addSubview:self.viewsForOptions[self.selectedIndex]]; 
    NSLog(@"%d", cell.contentView.subviews.count); 

    return cell; 
} 

上述代码针对cellForRowAtIndexPath中的特定部分和行进行调用。只要分段控制对象的值发生更改,该单元格就会更改。将子视图添加到cell.contentView

的方法是如下:

- (void)testOptionsValueChanged:(id)sender 
{ 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; 
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

我只有两个细胞; 1,因此cell == nil条件始终为false

问题:

当的tableView加载控制台日志:

0 
1 

当我改变分段控制的价值,我仍然得到:

0 
1 

经过一番更多的尝试,我基本上有第二个视图(第二个索引)的高度增加。我似乎无法得知这是真的发生的原因。

编辑:

其他代码片段:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 0) 
    { 
     // test detail view is a constant height 
     if (indexPath.row == 0) 
      return 180.0; 
    } 
    else if (indexPath.section == 1) 
    { 
     // based on the view loaded from the viewForOptions array 
     if (indexPath.row == 0) 
     { 
      return ((UIView *)self.viewsForOptions[self.selectedIndex]).frame.size.height; 
     } 
    } 

    return 0; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *DetailCellIdentifier = @"DetailCell"; 
    static NSString *InfoCellIdentifier = @"InfoCell"; 

    UITableViewCell *cell; 

    if (indexPath.section == 0) 
    { 
     // displays views and test button 
     if (indexPath.row == 0) 
     { 
      cell = [self cellForTestDetailWithCellIdentifier:DetailCellIdentifier forIndexPath:indexPath inTableView:tableView]; 
     } 
    } 
    else if (indexPath.section == 1) 
    { 
     // display the view for information based on segmented control 
     if (indexPath.row == 0) 
     { 
      cell = [self cellForInfoWithCellIdentifier:InfoCellIdentifier forIndexPath:indexPath inTableView:tableView]; 
     } 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 

- (void)viewDidLoad 
{ 
    // set the selected index for the options segmented control 
    self.selectedIndex = 0; 

    // instantiate all the views for test segmented control options 
    self.aViewController = [[AViewController alloc] ...]; 
    self.bViewController = [[BViewController alloc] ...]; 
    self.cViewController = [[CViewController alloc] ...]; 

    // add all the views to an array that will be used by the tableview 
    self.viewsForOptions = @[self.aViewController, self.bViewController, self.cViewController]; 

} 
+0

cell ==由于您使用的出队方法,nil条件始终为false,而不是段/单元的数量。 – Wain

+0

但是那是错的? – p0lAris

+0

不,我不确定你身高的评论在哪里,你实际看到了什么问题... – Wain

回答

2

0,11,1由表视图重用机制造成的。基本上,表格不会重复使用已经使用的任何单元格。因此,当您第一次填充并首先刷新表格时,将创建新的单元格。之后,重用队列中有足够多的单元未被使用,因此不需要创建新单元(滚动可能会创建更多)。

您的身高问题可能是由自动调整大小/布局造成的。当你添加子视图时,你应该指定它应该是什么尺寸,以及在超视图(单元)尺寸改变时应该如何改变这个尺寸。并且单元格大小发生了变化(添加子视图时记录它)。


单元的高度是一个部分。通常你会想要设置:

UIView *subview = self.viewsForOptions[self.selectedIndex]; 
subview.frame = cell.contentView.bounds; 
[cell.contentView addSubview:subview]; 

因此,当单元格的大小调整后,子视图将具有正确的大小。但这取决于您的自动调整大小规则。如果您设置布局约束来固定高度和宽度,则不需要设置框架。

无论哪种情况,您都需要指定超视图框架更改时子视图框架发生的情况。


我想你的问题是,单元格被重新调整大小,然后重用和您的子视图仍然附加。所以,它也会调整大小。然后,在heightForRowAtIndexPath:中使用子视图的高度(现在无效,尝试记录它)设置行的高度。

我想看看如何改变heightForRowAtIndexPath:的实现,以使用基于选定片段而不是子视图帧高度的配置。

+0

谢谢。但是,我真的不明白最后一部分。我已经在'heightForRowAtIndexPath'委托方法中指定了高度。我不确定你到底在说什么。你能否详细说明一下? 另外,如果我可能会问,是使用出队创建的新单元吗? 顺便提一句,它是0,1和0,1,表示segmentedControl值发生更改时,单元格不包含任何子视图。但我相信这就是你的意思。 – p0lAris

+0

其实,有些人可以围绕你所说的话真的有帮助。谢谢! – p0lAris

+0

已编辑。是的,该单元是由出列创建的。 – Wain

相关问题