簡體   English   中英

在TableView中選擇一個單元格並更改大小

[英]Select a Cell in a TableView and changed 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

如果您使用的是自動布局,則只需添加

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

如果不使用自動布局

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

采取

一個屬性selectedIndexPath

@property (strong, nonatomic) NSIndexPath * selectedIndexPath;

設置在

- (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