在单元格选择上向UITableViewcell动态添加新对象

时间:2015-03-13 08:33:10

标签: ios objective-c uitableview

我正在使用UITableView显示一行中每个团队的分数列表。我希望这样当用户选择任何特定的单元格(团队)来查看分数时,单元格的高度变为双倍,因此所选团队的分数变得可见。我能够在选择和调整时调整行高。取消选择但无法动态添加标签以列出分数。请为此方案建议一些解决方案。我们如何在didSelectRowAtIndexPath上动态地将对象(标签)添加到uitableviewcell?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_balls count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cell for row at index path");
    static NSString *cellIdentifier = @"Cell";

    CommentaryTableCell *tableCell = (CommentaryTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

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

    // Configure the cell...
    /*
    if(indexPath.row % 2 == 0)
        tableCell.backgroundColor = [self colorWithHexString:@"EAEAEA"];
    else
        tableCell.backgroundColor = [UIColor clearColor];

    */
    NSString *over = [_balls objectAtIndex:indexPath.row];
    NSArray *overData = [_ballsPerOver objectForKey:over];
    NSString *ball1 = [overData objectAtIndex:0];
    NSString *ball2 = [overData objectAtIndex:1];
    NSString *ball3 = [overData objectAtIndex:2];
    NSString *ball4 = [overData objectAtIndex:3];
    NSString *ball5 = [overData objectAtIndex:4];
    NSString *ball6 = [overData objectAtIndex:5];
    tableCell.overLabel.text = over;
    tableCell.ball1.image = [UIImage imageNamed:ball1];
    tableCell.ball2.image = [UIImage imageNamed:ball2];
    tableCell.ball3.image = [UIImage imageNamed:ball3];
    tableCell.ball4.image = [UIImage imageNamed:ball4];
    tableCell.ball5.image = [UIImage imageNamed:ball5];
    tableCell.ball6.image = [UIImage imageNamed:ball6];

    tableCell.teamLabel.text  = @"IND";
    tableCell.ball1Label.text = ball1;
    tableCell.ball2Label.text = ball2;
    tableCell.ball3Label.text = ball3;

    tableCell.videoThumbnail.image = [UIImage imageNamed:@"th1.jpg"];
    return tableCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int rowHeight;

    if ([indexPath row] == self.currentSelection)
    {
        rowHeight = self.newCellHeight;
    }
    else
    {
        rowHeight = 44.0f;
    }
    return rowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // do things with your cell here

    static NSString *cellIdentifier = @"Cell";

    CommentaryTableCell *tableCell = (CommentaryTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    // set selection
    self.currentSelection = indexPath.row;

    // save height for full text label
    if(!self.cellSelected)
    {
        NSLog(@"when selected");
        self.newCellHeight = (tableCell.frame.size.height) * 6;
        self.cellSelected = true;
        self.cellloadedOnce = true;
        tableCell.videoThumbnail.hidden = false;
    }
    else
    {
        NSLog(@"when de selected");
        self.newCellHeight = 44;
        self.cellSelected = false;
        self.cellloadedOnce = false;
    }

    // animate
    [tableView beginUpdates];
    [tableView endUpdates];
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    // do things with your cell here

    NSLog(@"when De selected at row index");
    // sentinel
    self.currentSelection = -1;

    // animate
    [tableView beginUpdates];
    [tableView endUpdates];
}

1 个答案:

答案 0 :(得分:0)

我不认为在同一个UITableViewCell上显示它是一个很好的解决方案。我建议使用另一个概念,并使用导航推送在另一个UITableView中显示所有分数..

如果你真的想这样做,你可以随时显示所有UITableViewCell的分数,只需在点击时调整单元格的高度?