2012-08-29 37 views
6

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

我提到不同的articals,试图改变颜色设置界限不适合我。 我在这个问题iphone uitablecellview overflow问了同样的问题,试过了答案,但它没有奏效。

所有我需要的是如何隐藏自定义单元格的底部部分,当它没有扩展时,并显示它时扩展...!

这是我的屏幕截图

When it is loaded 当加载 When I click on a cell 当我点击单元格] When I click on the 2nd cell 当我点击第二小区 When I click on the cell which is expanded
当我点击的细胞,其已经扩大了 这些是我用过的代码片段....

// 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"; 
    [email protected]"This Should be hidden.. Only shown when expanded"; 
    cell.productTargetLable.text= @"This Should be hidden.. Only shown when expanded"; 
    [email protected]"This Should be hidden.. Only shown when expanded"; 
    [email protected]"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; 
} 

回答

1

好吧,我得到它通过使用一个单一的标志和didSelectRowAtIndexPath方法来的cellForRowAtIndexPath单元格的引用来完成。并根据崩溃和拉伸设置我的标签可见和隐藏。标志随着细胞是否被选中而相应更新...... !!

感谢您的帮助大家..!

0

Ooops,代码负载使您的帖子变得肮脏。

我不会通过你的代码。

我会解释你的逻辑。

  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"; 
[email protected]"This Should be hidden.. Only shown when expanded"; 
cell.productTargetLable.text= @"This Should be hidden.. Only shown when expanded"; 
[email protected]"This Should be hidden.. Only shown when expanded"; 
[email protected]"This Should be hidden.. Only shown when expanded";; 

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

我仍然困惑,为什么你在做这些长编码,Please refer this answer for better explanation

+0

没有你有问题错了......我不需要将这添加到原始0 ...这些细节在我的表格的每个单元格中..我需要在自定义单元格底部显示这些细节......但这部分应该是当我实际上点击单元格时显示。仅用于测试目的,我正在添加这些字符串... – Gihan