2015-04-24 113 views
0

我使用此方法在选中它时放大了我的单元格的高度。当选择UITableView时使内容可见

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView beginUpdates]; 
    [tableView endUpdates]; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if([indexPath isEqual:[tableView indexPathForSelectedRow]]) { 
     return 100.0; 
    } 

    return 44.0; 
} 

我想显示当细胞被放大,这只是可见的区域UIImageView,我希望在CEL被取消它被隐藏。

+0

你到底需要什么帮助?你的代码似乎可以让选中的单元变大。只需将图像添加到cellForRowAtIndexPath中单元格的底部,并且只能在展开单元格时显示该图像。 – pteofil

回答

1

您的代码看起来是正确的放大单元格。

您也可以通过以下按照同样提到的步骤,以及 -

1)添加一个属性来跟踪选定单元格

@property (nonatomic) int currentSelectedRow; 

2)用一个值初始化它

self.currentSelectedRow = -1; 

3)返回单元高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if([indexPath row] == self.currentSelectedRow) { 
      return 100.0; 
} 

    return 44.0; 
} 

4)设置在小区选择方法所选择的行

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     // code to do on cell selection 

     // set the row number of current selected cell 
     self.currentSelectedRow = indexPath.row; 

     // animate the selected row 
     [tableView beginUpdates]; 
     [tableView endUpdates]; 
} 

5)在细胞取消选择方法

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{  

     // invalid row number 
     self.currentSelectedRow = -1; 

     // animate the selected row 
     [tableView beginUpdates]; 
     [tableView endUpdates]; 
} 

设置所选择的行,以无效行号(-1) :这只是一个示例代码,您可以根据您的要求对其进行定制。

1

下面是简单的例子

NSInteger selectedIndex; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UITableView *tbl=[[UITableView alloc]initWithFrame:CGRectMake(0, 75, 320, 300)]; 
    [tbl setDelegate:self]; 
    [tbl setDataSource:self]; 
    [self.view addSubview:tbl]; 

    selectedIndex=-1; 
} 


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 8; 
} 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell=[[UITableViewCell alloc]init]; 

    UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 70, 40)]; 
    [lbl setText:@"cell"]; 
    [cell addSubview:lbl ]; 

    UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(100, 6, 50, 30)]; 
    [imgVw setBackgroundColor:[UIColor clearColor]]; 
    [imgVw setTag:400+indexPath.row]; 
    [imgVw setAlpha:0.0]; 
    [cell addSubview:imgVw]; 

    return cell; 

} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; 

    [tableView beginUpdates]; 

    UIImageView *imgVwCellBg=(UIImageView*)[cell viewWithTag:400+indexPath.row]; 

    if (selectedIndex==indexPath.row) 
    { 
     selectedIndex=-1; 
     [imgVwCellBg setAlpha:0.0]; 
    } 
    else 
    { 
     selectedIndex=indexPath.row; 
     [imgVwCellBg setBackgroundColor:[UIColor brownColor]]; 
     [imgVwCellBg setAlpha:1.0]; 
    } 

    [tableView endUpdates]; 
} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; 
    UIImageView *imgVwCellBg=(UIImageView*)[cell viewWithTag:400+indexPath.row]; 

    selectedIndex=-1; 

    [imgVwCellBg setAlpha:0.0]; 

} 


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

    if(selectedIndex==indexPath.row) 
    { 
     return 100; 
    } 
    else 
    { 
     return 44; 
    } 

} 
1

试试这一次

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 4; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell; 
    if (cell == nil) { 
     cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"]; 
    } 

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 200, 0)]; 
    [imageView setTag:1000]; 
    [imageView setImage:[UIImage imageNamed:@"Image_2"]]; 
    [cell addSubview:imageView]; 

    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 

    [tableView beginUpdates]; 
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1000]; 

    if (selectedIndex==indexPath.row) 
    { 
     selectedIndex=-1; 
     [UIView animateWithDuration:0.4 animations:^{ 
     [imageView setFrame:CGRectMake(20, 5, 200, 0)]; 
     }]; 
    } 
    else 
    { 
     selectedIndex = indexPath.row; 
     [UIView animateWithDuration:0.4 animations:^{ 
     [imageView setFrame:CGRectMake(20, 5, 200, 170)]; 
     }]; 
    } 
    [tableView endUpdates]; 

} 

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 

    [tableView beginUpdates]; 

    [UIView animateWithDuration:0.4 animations:^{ 
    UIImageView *imageView = (UIImageView *)[cell viewWithTag:1000]; 
    [imageView setFrame:CGRectMake(20, 5, 200, 0)]; 
}]; 

    [tableView endUpdates]; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(selectedIndex == indexPath.row) 
    { 
     return 200; 
    } 
    else 
    { 
     return 75; 
    } 
    return 0; 
} 
+0

是'selectedIndex'一个NSDictionary,NSArray或NSIndexPath? – 000

+0

这是NSInteger。 NSInteger selectedIndex; – Dev

1

我认为问题的NSIndexPath也许isEuqal:方法,不喜欢的NSString,你不能只是用户isEuqal:判断NSIndexPath等于。

if([indexPath isEqual:[tableView indexPathForSelectedRow]]) { 

NSIndexPath *selectedPath = [tableView indexPathForSelectedRow]; 
if(selectedPath.row == indexPath.row && selectedPath.section == indexPath.section){ 
0

好了,所以这是我做了更换,使用你给我和所有做

@property (nonatomic) int currentSelectedRow; 
- (void)viewDidLoad { 
[super viewDidLoad]; 
self.currentSelectedRow = -1; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString * cellId = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath]; 
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304]; 
contentView.hidden = YES; 



return cell; 

} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 
self.currentSelectedRow = indexPath.row; 



[tableView beginUpdates]; 
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304]; 
contentView.hidden = NO; 

if (_currentSelectedRow == indexPath.row) 
{ 

    [UIView animateWithDuration:0.4 animations:^{ 

     [contentView setFrame:CGRectMake(20, 5, 200, 0)]; 
    }]; 
} 
else 
{ 
    _currentSelectedRow = indexPath.row; 
    [UIView animateWithDuration:0.4 animations:^{ 
     contentView.hidden = NO; 
     [contentView setFrame:CGRectMake(20, 5, 200, 170)]; 
    }]; 
} 

[tableView endUpdates]; 



} 
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; 
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304]; 
contentView.hidden = YES; 

// invalid row number 
self.currentSelectedRow = -1; 

// animate the selected row 
[tableView beginUpdates]; 
[tableView endUpdates]; 
} 


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
if([indexPath row] == self.currentSelectedRow) { 
    return 100.0; 
} 

return 44.0; 
} 

感谢您的所有输入 一定要投票问题它会帮助很多

相关问题