1
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return 50; 
} 

在这个地方,我想动态添加一个按钮来增加单元格的高度,所以当用户单击该按钮时,它应该增加单元格的高度,然后再次单击以调整高度。

我想要类似的东西:

-(void)IncreaseCell
{
    UIButton *DownArrow = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [DownArrow addTarget:self 
                  action:@selector(IncreaseCell:)
        forControlEvents:UIControlEventTouchDown];
    //[DownArrow setTitle:@"Arrow" forState:UIControlStateNormal];
    DownArrow.frame = CGRectMake(121.0, 112.0, 72.0, 37.0);
    [cell.contentView addSubview:DownArrow];

    UIImage *buttonDown = [UIImage imageNamed:@"friendsDownArrowButton.png"];
    [DownArrow setBackgroundImage:buttonDown forState:UIControlStateNormal];
    [cell.contentView addSubview:DownArrow];   
}
4

4 回答 4

1
  1. 您将需要创建一个NSMutableArray作为实例变量,您可以在其中跟踪您想要“增加”的所有单元格。

    @interface YourTableViewController : UITableViewController {
        NSMutableDictionary *increasedRows;
    }
    

    请记住分配/初始化该变量。

  2. 为了让你的细胞增加:

    -(void)increseCell: (BOOL)status forIndexPath: (NSIndexPath*)indexPath {
        [increasedRows setObject:[NSNumber numberWithBool:status] forKey: indexPath];
    
        [self.tableView beginUpdates]; //Delete if you don't want it animated
        [self.tableView endUpdates]; //Delete if you don't want it animated
    
        // [self.tableView reloadData]; //Uncomment if you don't want it animated
    }
    
  3. 您更改tableView: heightForRowAtIndexPath:声明以检查此字典中的 indexPath。

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        // Check if cell is increased, return custom height
        if([[increasedRows objectForKey:indexPath] boolValue]) {
            return 150;
        }
        // Cell isn't increased so return regular height
        return 50;
    }
    

此方法将允许您单独为每一行执行此操作,并允许您对其进行动画处理。

于 2013-08-31T11:13:43.323 回答
0

what i did in one of my project is i created a custom cell and add subview to it statically

1 st scene non collapsed view custom cell shows only button on click of which it will show extended view by increasing size of cell..

i.e on click of button you should increse size of cell using function..

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

 BOOL check=[[booleanArrayofrows objectAtIndex:indexPath.row] boolValue];

        if (check)
        {
            return 180; ..expanded view height
        }
        return 40; collapse view height
    } 
}
于 2013-09-02T11:17:31.730 回答
0

您所要做的就是声明一个变量:

@interface yourViewController ()
{
    CGFloat cellSize;
}

在你的 heightForRowAtIndexPath:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
       return cellSize; 
}  

在你的viewDidLoad

-(void)viewDidLoad
{
     [super viewDidLoad];
     cellSize = 50;
}

最后在你的increaseCell:

-(void)IncreseCell {
     cellSize += 10;
     [self reloadData];
}
于 2013-08-31T10:48:24.033 回答
0

您需要更改heightForRowAtIndexPath.

添加一些存储,如数组,其中包含指示行是否扩展的标志。点击按钮时,获取它所在单元格的索引路径并编辑数组内容以设置/取消设置标志。重新加载表视图(或更改的索引路径的行)。

于 2013-08-31T10:52:16.570 回答