简体   繁体   English

在TableView中选择一个单元格并更改大小

[英]Select a Cell in a TableView and changed size

I need to set my UITableView cells, when I select a cell increases its size to twice, and if I select another this increase in size and decrease that was previously selected size. 我需要设置我的UITableView单元格,当我选择一个单元格时,它将其大小增加到两倍,如果我选择了另一个,则该大小增加并减小了以前选择的大小。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.states count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"SimpleTableCell";

    SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.stateName.text = [self.states objectAtIndex:indexPath.row];
    return cell;
}

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

@end

If you are using autolayout then simply add 如果您使用的是自动布局,则只需添加

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // for particular index 
    return UITableViewAutomaticDimension;
}

and if not using autolayout then 如果不使用自动布局

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(indexPath.row == 1){
           return 80;
        }else{
          return 44;
        }
    }

Take 采取

one property selectedIndexPath 一个属性selectedIndexPath

@property (strong, nonatomic) NSIndexPath * selectedIndexPath;

set that in 设置在

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


-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndexPath != nil && indexPath == selectedIndexPath){
        return 80;
}

    return 40;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM