2015-04-24 46 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; 
} 

感謝您的所有輸入 一定要投票問題它會幫助很多

相關問題