2016-03-03 63 views
0

我需要设置我的UITableView单元格,当我选择一个单元格时将其大小增加到两倍,如果我选择另一个,则增加大小并减小先前选定的大小。在TableView中选择一个单元格并更改大小

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

回答

0

采取

一个属性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; 
} 
+0

如何申报selectedindexpath标识? –

+0

在h文件中创建属性,如 – techloverr

+0

@property(强,非原子)NSIndexPath * selectedIndexPath; – techloverr

1

如果您正在使用自动布局,然后在不使用自动布局简单地添加

-(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; 
     } 
    } 
+0

我不使用自动布局,我想我需要使用didselect什么 –

相关问题