我想隱藏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單元。
第1步:要有禮貌,並確保您出示您的代碼在良好的格式化的方式。 –
使用標籤訪問您的按鈕,然後使用設置「隱藏」屬性爲YES。 – WasimSafdar
@WasimSafdar不要使用此標籤 –