2012-06-04 72 views
0

有没有办法根据点击单元格来改变uitableview的单元格大小?例如:宽度为100.0f的单元格,首先单击单元格将宽度更改为150.0f,然后单击单元格宽度更改为100.0f。我怎样才能做到这一点?在uitableviewcell上点击多次

回答

1

你应该能够用NSIndexPath对象索引一个NSMutableDictionary。这可以让你缓存你想要的点击TVCells的大小。然后,只需将它们拉出来在运行时:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSNumber *newHeight; 
    NSNumber *height = [[self heightCache] objectForKey:indexPath]; 

    if (height == nil) newHeight = [NSNumber numberWithFloat:100.0]; 
    else newHeight = [NSNumber numberWithFloat:[height doubleValue] + 50]; 
    [[self heightCache] setObject:newHeight forKey:indexPath]; 
    [tableView reloadData]; // also consider reloadRowsAtIndexPaths: 
} 

与此同时,在您的委托:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSNumber height = [heightCache objectForKey:indexPath]; 
    return height == nil ? defaultHeight : [height floatValue]; 
} 

UPDATE:哦,哎呀,我还以为你的意思是TVCells只是越做越大。是的,只需将我的示例中的浮点数更改为bools,然后再根据布尔值返回高度。

1

您需要维护一个布尔值,在随后的每次点击中更改此布尔值。

现在基于此布尔定义您的cellForRowAtIndexPathmethoddidSelectRowAtIndexPath方法只需更新布尔和重新加载table