【问题标题】:UItable Cell Overflow iosUItable 单元格溢出 ios
【发布时间】:2012-08-29 10:46:36
【问题描述】:

我正在做一个应用程序,它需要在单击时扩展单元格以显示更多详细信息。我使用了一个自定义单元并将它们添加到 UItableview 中。当我单击单元格时,它的动画效果很好并下降,当我再次单击时它上升,这是通过更改单元格的高度来完成的。实际的自定义单元格大小比我通常显示的单元格大。当我单击单元格时,会显示整个单元格。我遇到的唯一问题是数据溢出。未选择单元格时应隐藏这些数据。只有当它被选中时,才应该显示这些数据。

我参考了不同的文章,尝试更改颜色设置绑定对我不起作用。 我在这个问题iphone uitablecellview overflow 中提出了同样的问题,尝试了答案,但没有奏效。

我只需要在自定义单元格未扩展时如何隐藏它的底部,并在它扩展时显示它......!

这些是我的屏幕截图

加载时 当我点击一个单元格] 当我单击第二个单元格时
当我点击已经展开的单元格时 这些是我用过的代码片段......

// Declaring the SearchResultTable.
    CGRect filterFrame = CGRectMake(0,31, 320, 400);
    self.searchResultTable = [[UITableView alloc] initWithFrame:filterFrame];
    self.searchResultTable.dataSource = self;
    self.searchResultTable.delegate = self;
    self.searchResultTable.backgroundColor = [UIColor whiteColor];
    self.searchResultTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.searchResultTable.separatorColor = [UIColor lightGrayColor];
    [self.view addSubview:self.searchResultTable];

//Adding cells
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"ExpandedSearchResultTableCell";


    ExpandedSearchResultTableCell *cell = (ExpandedSearchResultTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil)
    {
        cell.contentView.clipsToBounds = YES;
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExpandedSearchResultTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];


    }

    cell.productNameLable.text = @"Only this should be shown";
    cell.productListLabel.text = @"This Should be hidden.. Only shown when expanded";
    cell.productApplicationLable.text=@"This Should be hidden.. Only shown when expanded";
    cell.productTargetLable.text= @"This Should be hidden.. Only shown when expanded";
    cell.productQuantityLable.text=@"This Should be hidden.. Only shown when expanded";
    cell.productReactivityLable.text=@"This Should be hidden.. Only shown when expanded";;


    return cell;
}


//on click event
(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {




    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];

    // Toggle 'selected' state
    BOOL isSelected = ![self cellIsSelected:indexPath];

    // Store cell 'selected' state keyed on indexPath
    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
    [selectedIndexes setObject:selectedIndex forKey:indexPath]; 

    // This is where magic happens...
    [searchResultTable beginUpdates];
    [searchResultTable endUpdates];
}

//Getting the height depending on expanded or not
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // If our cell is selected, return double height
    if([self cellIsSelected:indexPath]) {
        return kCellHeight * 3.0;
    }

    // Cell isn't selected so return single height
    return kCellHeight;
}

【问题讨论】:

    标签: objective-c ios uitableview ios-4.2 custom-cell


    【解决方案1】:

    如果您正在寻找普通 tableview 类型的单元格扩展,

    iPhone UITableView with animated expanding cells

    如果您使用分组表视图并希望扩大节标题的选择范围,

    Table View Animations and Gestures

    【讨论】:

    • 我已经完成了动画部分..我只需要知道如何隐藏单元格的某些部分...无论如何我会参考你的链接...我已经检查了第一个一个
    【解决方案2】:

    好的,我通过在 didSelectRowAtIndexPath 中使用单个标志和对 cellForRowAtIndexPath 单元格的引用来完成它。并根据折叠和拉伸设置我的标签可见和隐藏。标记会随着单元格是否被选中而相应更新...!!

    感谢大家的帮助..!

    【讨论】:

      【解决方案3】:

      糟糕,大量代码弄脏了您的帖子。

      我不会通过你的代码。

      我会解释你的逻辑。

      1. 只有应该显示的才会进入你的 indexpath.row = 0;
      2. 如果选择部分中的行数,则显示单元格的计数+ 1,否则为1。
      cell.productNameLable.text = @"Only this should be shown";
      cell.productListLabel.text = @"This Should be hidden.. Only shown when expanded";
      cell.productApplicationLable.text=@"This Should be hidden.. Only shown when expanded";
      cell.productTargetLable.text= @"This Should be hidden.. Only shown when expanded";
      cell.productQuantityLable.text=@"This Should be hidden.. Only shown when expanded";
      cell.productReactivityLable.text=@"This Should be hidden.. Only shown when expanded";;
      

      这会使您的代码变脏。 如果您要在一个单元格中显示所有数据,并且当您选择一个部分时只添加一个单元格,我建议将所有这些添加到不同的单元格中。

      我仍然很困惑你为什么要写这么长的代码,Please refer this answer for better explanation

      【讨论】:

      • 不,你的问题错了...我不需要将其添加到原始 0...这些详细信息在我表格的每个单元格中...我需要在自定义中显示这些详细信息单元格底部部分...但是当我实际单击单元格时应该显示这部分..仅出于测试目的,我添加了这些字符串...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 2013-05-08
      • 1970-01-01
      相关资源
      最近更新 更多