Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在UITableView中相互显示UIButton_Objective C_Uitableview - Fatal编程技术网

Objective c 在UITableView中相互显示UIButton

Objective c 在UITableView中相互显示UIButton,objective-c,uitableview,Objective C,Uitableview,我希望仅在UITableView中的偶数indexath.row处显示UIButton 但是,UIButton出现在所有单元格中 if (indexPath.row % 2 == 0) { UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(160, 140, 80, 40); [button setTitle:@"button" f

我希望仅在UITableView中的偶数indexath.row处显示UIButton

但是,UIButton出现在所有单元格中

if (indexPath.row % 2 == 0) {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(160, 140, 80, 40);
    [button setTitle:@"button" forState:UIControlStateNormal];
    [cell addSubview:button];
}
我如何解决这个问题?
谢谢。

您的代码是正确的,但您犯了一个很小的错误。您正在尝试在
单元格
上添加按钮,而不是这样做,请尝试在
单元格
contentView
上添加按钮,如下所示

[cell.contentView addSubview:button];

首先确定单元格的高度:

由于按钮的高度,您可能面临此问题。 它应该大于或等于按钮的高度。 另外,用这个替换代码

if (indexPath.row % 2 == 0) {
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(160, 4, 80, 40)];
        [button setTitle:@"BtnName" forState:UIControlStateNormal];
        [cell.contentView addSubview:button];

}

[cell.contentView addSubview:按钮]