我正在尝试使用每个部分中的4个单元格来实现表格视图,并且部分的数量也是2.我在每个部分的最后两行中添加了购买按钮并使其隐藏直到未选择行。当我点击第一部分的最后一行时,会出现购买按钮。但是,一旦我点击第二部分的最后一行(最后一部分),它就不会出现。这是我的代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
// some code for cell properties
if(indexPath.section==0)
{
if(indexPath.row==2)
{
//some code for property of buy button1
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = buyButton1;
[buyButton1 setHidden:YES];
}
if(indexPath.row==3)
{
//some code for property of buy button2
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = buyButton2;
[buyButton2 setHidden:YES];
}
}
if(indexPath.section==1)
{
if(indexPath.row==2)
{
//some code for property of buy button3
[cell addSubview:buyButton3];
[buyButton3 setHidden:TRUE];
}
if(indexPath.row==3)
{
buyButton4= [UIButton buttonWithType:UIButtonTypeRoundedRect];
buyButton4.frame = CGRectMake(194, 4, 70, 37);
[buyButton4 setTitle:@"Buy" forState:UIControlStateNormal];
[buyButton4 addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
buyButton4.tag = 4;
[cell addSubview:buyButton4];
[buyButton4 setHidden:TRUE];
}
}
}
if(indexPath.section == 0)
{
cell.textLabel.text = [privateArray objectAtIndex:indexPath.row];
}
else
{
cell.textLabel.text = [workArray objectAtIndex:indexPath.row];
}
return cell;
}
这是我选择行
的代码- (void)tableView:(UITableView *)tableview didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableViewForOccasion deselectRowAtIndexPath:indexPath animated:NO];
NSArray *visibleCells = [tableViewForOccasion visibleCells];
for (UITableViewCell *aCell in visibleCells)
{
aCell.accessoryType = UITableViewCellAccessoryNone;
}
if(indexPath.section==1)
{
if(indexPath.row==3)
{
[buyButton4 setHidden:FALSE];
}
}
}
答案 0 :(得分:2)
因为你在谈论第二部分的最后一行,那么你的检查条件是错误的
if(indexPath.section==1)
{
if(indexPath.row==3) // you have mistake in this part where you had if(indexPath.row==2)
{
[buyButton4 setHidden:FALSE];
}
}
答案 1 :(得分:0)
I think you forgot the `buyButton4` allocation and `setHidden = FALSE`.
//Replace Your Code
if(indexPath.row==3)
{
UIButton * buyButton4 =[[UIButton alloc] init];
buyButton4= [UIButton buttonWithType:UIButtonTypeRoundedRect];
buyButton4.frame = CGRectMake(194, 4, 70, 37);
[buyButton4 setTitle:@"Buy" forState:UIControlStateNormal];
[buyButton4 addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
buyButton4.tag = 4;
[cell addSubview:buyButton4];
[buyButton4 setHidden:FALSE];
}