如何在Objective C的动态UITableViewCell中隐藏特定的行按钮?

How to hide a particular row button in Dynamic UITableViewCell in Objective C?

提问人:Shubam Gupta 提问时间:12/16/2017 最后编辑:QHarrShubam Gupta 更新时间:7/23/2018 访问量:1180

问:

我已经实现了动态 TableView、Textfield 和 Buttons。我的问题是,当我将按钮隐藏在我的第一行时,其他五行单元格按钮也被隐藏了。UITableViewCell

任何人都可以为这个问题提出解决方案吗?

我尝试了以下代码..

ladiesdetails=[[NSMutableArray alloc]initWithObjects:@"2",@"0",@"0",@"0",@"0",@"0", nil];



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier = @"cell1";

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[passengerdetailcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
if([[ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.malebutton.hidden=yes;
}
return cell;
}
iOS Objective-C iPhone UITableView iOS7

评论

0赞 Nirav Kotecha 12/16/2017
当您隐藏 MaleButton 时,您还必须显示其他索引路径。写入 if 条件的 else 部分并尝试。indexPath.row==2hidden=no;

答:

1赞 Suhit Patil 12/16/2017 #1

只需放置 else 条件并使按钮在 cellForRowAtIndexPath 方法中可见即可。如果您有任何其他条件要显示,也请添加。

if([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2) {
   cell.malebutton.hidden = YES;
} else {
   cell.malebutton.hidden = NO;
}
0赞 Abhishek Thapliyal 12/16/2017 #2

喜欢这个

bool flag = ([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2)
cell.malebutton.hidden = flag

评论

3赞 dustin.schultz 12/17/2017
简洁是可以接受的,但更完整的解释更好。
0赞 Vinodh 12/17/2017 #3

发生这种情况是因为单元格重用,您使用分配单元格,如下所示。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        // Return the number of rows in the section.
        return 6;
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        PassengerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PassengerDetailCell" forIndexPath:indexPath];
        if([[self.ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
        {
            cell.maleButton.hidden = TRUE;
        }
        return cell;
    }

评论

0赞 Shubam Gupta 12/18/2017
PassengerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@“PassengerDetailCell” forIndexPath:indexPath];此行显示类似 NSInternalConsistencyyException:unable to dequeue the cell 的错误。为什么会出现此错误
0赞 Vinodh 12/19/2017
检查是否导入了正确的标头和类名称相同,我的代码的类命名会有所不同