【问题标题】:get cell from indexPath for expand the row when clicked?单击时从 indexPath 获取单元格以扩展行?
【发布时间】:2014-12-25 22:33:24
【问题描述】:

我知道有一个名为[tableView cellForRowAtIndexPath:indexPath]; 的方法,我们可以使用它来获取给定 indexPath 处的单元格。但是我想在行点击时扩展行。使用此代码-

    - (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];
        int sec = indexPath.section;
        int row = indexPath.row;
        NSString* indexpathStr = [NSString stringWithFormat:@"%d-%d", sec, row];

        [selectedIndexes setObject:selectedIndex forKey:indexpathStr];
        // This is where magic happens...
        [tableView beginUpdates];
        [tableView endUpdates];

    }
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        CGFloat height = 0.0;
        if([self cellIsSelected:indexPath]) {
//problem here
            CalEventCell* cell = (CalEventCell*)[tableView cellForRowAtIndexPath:indexPath]; 
            [self setDescriptionFrame:cell.descriptionLbl];
            height = cell.descriptionLbl.frame.size.height+cell.eventLbl.frame.size.height +20;
        }
        else
            height = 60;
        return height;
    }

当点击一行时,我在 tableview 上调用 beginUpdatesendUpdates 方法,并在 heightForAtIndexPath 方法中更改行高。问题是,如您所见,我从 heightForAtIndexPath 调用 cellForAtIndexPath,而在 cellForRow 中有 @ 987654327@ 方法调用变成了对 heightForRow 的调用,这个循环永远不会结束,程序会因EXC-BAD-EXCEP 而崩溃。 那么从 indexPath 获取单元格的任何其他方法或任何其他方式呢?任何帮助将不胜感激。

【问题讨论】:

  • 从 didSelectRow 重新加载表并从那里获取 indexpath 并尝试仅为特定索引设置不同的高度。

标签: ios objective-c uitableview


【解决方案1】:
  • 获取一个数组来存储选定的索引,然后重新加载该 tableview。
  • 如果您点击该单元格高度将扩大,如果您再次点击它会降低其高度。

查看以下代码。

// Add a mutable array to your class
NSMutableArray *selectedCellArray;

// Initialise it in `viewDidLoad`
-(void)viewDidLoad
{
    [super viewDidLoad];
    selectedCellArray = [[NSMutableArray alloc] init];
}


// Add selected indices in selectedCellArray
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];
    [tableView reloadData];

     if (![selectedCellArray containsObject:indexPath]) 
         [selectedCellArray addObject:indexPath];
     else
         [selectedCellArray removeObject:indexPath];

    [tableView beginUpdates]; // Animate the height change
    [tableView endUpdates];
}

// Write the UI Updation in HeightForRowAtIndexPath Method
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 0.0;

    if (selectedCellArray.count>0) {
        if ([selectedCellArray containsObject:indexPath]) {
            CalEventCell* cell = (CalEventCell*)[tableView cellForRowAtIndexPath:indexPath];
            [self setDescriptionFrame:cell.descriptionLbl];
            height = cell.descriptionLbl.frame.size.height+cell.eventLbl.frame.size.height+20;

            return height;
        }else{
            height = 60.f;
            return height;
        }
    }
    return height;  
}

希望对您有所帮助

【讨论】:

    【解决方案2】:

    您应该在 didSelectRowAtIndexPath 方法中调用以下代码行

    [tableView beginUpdates];    
    [tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:row inSection:sec]] withRowAnimation:UITableViewRowAnimationNone];
    [tableView endUpdates];
    

    当重新加载行时,cellForRowAtIndexPath:indexPathheightForRowAtIndexPath 方法将被调用,您可以在其中扩展单元格的高度

    【讨论】:

      【解决方案3】:

      如果您使用可重复使用的单元格,则不能在 heightForRowAtIndexPath 中使用 cellForRowAtIndexPath。您需要以其他方式计算高度。可能对您有用的最简单方法是在您的类中保留一个原型单元格,仅用于调整大小。

      【讨论】:

        【解决方案4】:

        试试这个:

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {  
        
             [your_tableView reloadData];
        }
        

        【讨论】:

          猜你喜欢
          • 2017-01-27
          • 1970-01-01
          • 2019-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多