2016-07-15 120 views
0

我想隱藏UITableView某個單元格上的按鈕,但我無法找到我做錯了什麼,我的UITableView的某些單元格同時具有文本和按鈕,某些單元格只有單行或多行的文本。我想在UITableView的某個單元上顯示按鈕。我這樣做代碼:如何隱藏tablview特定單元格上的按鈕?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     NSString *cellidentifier= @"cell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath]; 

    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(5, 2, 100, 30); 
    [button setTitle:@"World" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside]; 
    button.backgroundColor= [UIColor clearColor]; 
    button.tag = indexPath.row; 
    [cell addSubview:button]; 

    if(indexPath.row ==0) 
    { 


     cell.textLabel.attributedText = [dataArray objectAtIndex:0]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
    if (indexPath.row == 1) 
    { 


    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[dataArray objectAtIndex:1]] placeholderImage:nil]; 
    cell.imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2); 
    } 
    if(indexPath.row ==2) 
    { 


     cell.textLabel.attributedText = [dataArray objectAtIndex:2]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
    if(indexPath.row ==3) 
    { 

     cell.textLabel.attributedText = [dataArray objectAtIndex:3]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
    } 
    if(indexPath.row ==4) 
    { 

     cell.textLabel.attributedText = [dataArray objectAtIndex:4]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
     cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     cell.textLabel.numberOfLines = 0; 


    } 
     if(indexPath.row == 5) 
    { 

     cell.textLabel.attributedText =[dataArray objectAtIndex:5]; 
     cell.textLabel.textAlignment = NSTextAlignmentRight; 
    } 


     if(indexPath.row == 6) 

    { 
     //[button setHidden:YES]; 
     cell.textLabel.attributedText = [dataArray objectAtIndex:6]; 
     cell.textLabel.textAlignment = NSTextAlignmentCenter; 
     cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; 
     cell.textLabel.numberOfLines = 0; 

    } 
    if (indexPath.row == 6 || indexPath.row == 5) 
    { 
     [button setHidden:NO]; 
    } 
    else { 
     [button setHidden:YES]; 
    } 
     return cell; 
    } 

但這個代碼將增加6日和5日細胞還對第二和第三個單元格按鈕也,我想按鈕僅在第6和第5單元。

+0

第1步:要有禮貌,並確保您出示您的代碼在良好的格式化的方式。 –

+0

使用標籤訪問您的按鈕,然後使用設置「隱藏」屬性爲YES。 – WasimSafdar

+0

@WasimSafdar不要使用此標籤 –

回答

4

你不該內cellForRowAtIndexPath添加一個按鈕[cell addSubView: button]。 dequeReusableCellForIndexPath向tableView請求一個單元格。 tableView創建一個,如果它沒有任何或傳遞一箇舊的。如果你收到舊的,那麼我可能已經有了一個按鈕。如果你添加另一個,你最終會得到一堆按鈕。

你可以達到你要從兩方面去看: 您可以在storyboard創建您的細胞,例如,添加按鈕,並在UITableViewCell子類創建一個出口。然後,您可以根據單元格隱藏或取消隱藏按鈕。另一種方式是,如果按鈕太不同,您可以在故事板中創建兩個單元格,使用不同的標識符,例如「cellWithButton」和「cellWithoutButton」。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UITableViewCell *cell; 

    if (indexPath.row < 5) { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithoutButton"]; 
    } else { 
     cell = [tableView dequeueReusableCellWithIdentifier:@"cellWithButton"]; 
    } 

    // Do the rest of your configuration 

    return cell; 
} 
+0

我厭倦了這一個也但它添加按鈕單元格第二和第三也單元格第六和第五 – Asmita

+0

讓我發表一條新評論 –

+1

正確的答案..甚至沒有閱讀其他:) –

0

我認爲你應該嘗試

在開始時添加按鈕只是稍後添加它。 一樣,

if (indexPath.row == 6 || indexPath.row == 5) 
{ 
    [cell addSubview:button]; 
} 
else { 
    [button removeFromSuperview]; 
} 
0

只要你可以通過

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];// or UIButtonTypeCustom 
button.frame = CGRectMake(5, 2, 100, 30); 
[button setTitle:@"World" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside]; 
button.titleLabel.textColor = [UIColor blueColor]; 
button.backgroundColor= [UIColor clearColor]; 
button.tag = indexPath.row; 
[cell.contentView addSubview:button]; 

if(indexPath.row==0) 
{ 
    button.hidden = YES; 
} 
else if(indexPath.row==1){ 
    button.hidden = YES; 
} 
else if(indexPath.row==2){ 
    button.hidden = YES; 
} 
else if(indexPath.row==3){ 
    button.hidden = YES; 
} 
else if(indexPath.row==4){ 
    button.hidden = YES; 
} 
else if(indexPath.row==5 || indexPath.row==6){ 
    button.hidden = NO; 
} 

否則

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // or UIButtonTypeCustom 
button.frame = CGRectMake(5, 2, 100, 30); 
[button setTitle:@"World" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside]; 
button.titleLabel.textColor = [UIColor blueColor]; 
button.backgroundColor= [UIColor clearColor]; 
button.tag = indexPath.row; 

if(indexPath.row==0) 
{ 

} 
else if(indexPath.row==1){ 

} 
else if(indexPath.row==2){ 

} 
else if(indexPath.row==3){ 

} 
else if(indexPath.row==4){ 

} 
else if(indexPath.row==5 || indexPath.row==6){ 
    [cell.contentView addSubview:button]; 
} 
+0

不工作這不知道爲什麼,我昨天做了你的第一個代碼,但它將該按鈕放在其對應的或乘數的其他單元上 – Asmita

+0

我的代碼肯定有效 – user3182143

+0

昨天你有沒有試過我的答案?我剛剛發佈了我的答案iAsmi。 – user3182143

0

使用做到這一點

UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];// or UIButtonTypeCustom 
button.frame = CGRectMake(5, 2, 100, 30); 
[button setTitle:@"World" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside]; 
button.titleLabel.textColor = [UIColor blueColor]; 
button.backgroundColor= [UIColor clearColor]; 
button.tag = indexPath.row; 
[cell.contentView addSubview:button]; 

if(indexPath.row==5 || indexPath.row==6){ 
    button.hidden = NO; 
} 
else { 
    button.hidden = YES; 
} 

它會消除你的代碼易讀了。

0
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
button.frame = CGRectMake(5, 2, 100, 30); 
    [button setTitle:@"World" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(openNewPage:) forControlEvents:UIControlEventTouchUpInside]; 
    button.backgroundColor= [UIColor clearColor]; 
    button.tag = indexPath.row; 
    //add here 
    button.hidden==YES; 
    [cell addSubview:button]; 

    and after write this condition 
    if (indexPath.row == 6 || indexPath.row == 5) 
    { 
    [button setHidden:NO]; 
    } 
0

你應該讓[細胞addSubview:按鈕]在第6和第5單元

相關問題