选择UITableView行时添加子视图



我正在开发一款iPhone应用程序,该应用程序将XML文件解析为UITableView。它列出了XML文件中项目的所有标题,当选择指定的单元格时,我正试图展开单元格并添加所选项目的正文内容。

我可以让单元格正确展开,但当我没有运气将正文内容添加为子视图时。当在cellForRowAtIndexPath填充单元格时,我可以添加正文内容,它显示得很好。

我的问题是,如何在选定的单元格中添加子视图?

我的cellForRowAtIndexPath功能,带有"正在运行"的bodyLabel注释:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
static NSString *cellID = @"Cell";
issue *curIssue = [[parser issues] objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];

    CGRect nameFrame = CGRectMake(0,2,300,15);
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:nameFrame];
    nameLabel.tag = 1;
    nameLabel.font = [UIFont boldSystemFontOfSize:12];
    [cell.contentView addSubview:nameLabel];
    CGRect bodyFrame = CGRectMake(0,16,300,60);
    UILabel *bodyLabel = [[UILabel alloc] initWithFrame:bodyFrame];
    bodyLabel.tag = 2;
    bodyLabel.numberOfLines = 10;
    bodyLabel.font = [UIFont systemFontOfSize:10];
    bodyLabel.hidden = YES;
    [cell.contentView addSubview:bodyLabel];
}

    UILabel *nameLabel = (UILabel *)[cell.contentView viewWithTag:1];
    nameLabel.text     = [curIssue name];
    UILabel *bodyLabel = (UILabel *)[cell.contentView viewWithTag:2];
    bodyLabel.text     = [curIssue body];
    return cell;
}

这是我的heightForRowAtIndexPath函数,我试图在其中添加子视图。当尝试执行时,我在尝试分配*bodyLabel元素时收到EXC_BAD_ACESS异常。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger defCellHeight  = 15;
NSInteger heightModifier = 10;
if([self cellIsSelected:indexPath]){
    return defCellHeight * heightModifier;
}
return defCellHeight;
}

我的didSelectRowAtIndexPath功能,允许细胞生长/收缩:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{     
    BOOL isSeld = ![self cellIsSelected:indexPath];
    NSNumber *seldIndex = [NSNumber numberWithBool:isSeld];
    [selectedIndexes setObject:seldIndex forKey:indexPath];
    UILabel *label = (UILabel *)[tableView viewWithTag:2];
    label.hidden = NO;
    [curIssView beginUpdates];
    [curIssView endUpdates];
}

最后,如果选择了单元格,则返回true的cellIsSelected辅助函数:

-(bool) cellIsSelected:(NSIndexPath *)indexPath
{
    NSNumber *selIndex = [selectedIndexes objectForKey:indexPath];
    return selIndex == nil ? FALSE : [selIndex boolValue];
}

你可以在这里找到完整的源文件。

似乎您正在多次分配和添加相同的UILabel。您应该只需要在cellForRowAtIndexPath方法中执行一次。您可能还希望在cellForRowAtIndexPath方法中将bodyLabel设置为隐藏,然后在选择单元格时将其设置为不隐藏。类似于:

bodyLabel.hidden = YES;

另一件事。为什么要取消选择didSelectRowAtIndexPath中的行?

为什么要在heightForRowAtIndexPath中创建UILabel?如果要将其添加到行中,请使用上面使用的cellForRowAtIndexPath方法。

只需在didSelectRowAtIndexPath中使用UIView的addSubview方法将子视图添加到UITableViewCell或视图中的任何其他位置,就可以在选择UITableView行时显示子视图。

要在选择单元格时将子视图添加到单元格中,非常简单:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{     
    /*
     .. your other setup
    */
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell.contentView addSubview:<your subview>];
}

最新更新