我想在每個部分實現4個單元格的表視圖,部分的數目也是2.我在每個部分的最後兩行添加了buy按鈕,使其隱藏,直到未選中行。當我點擊第一部分的最後一行時,出現購買按鈕。但是,只要我點擊第二部分(最後一部分)的最後一行,就不會出現。這是我的代碼。UIButton不會出現在UItableView的最後一節的最後一行
- (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];
}
}
}
ooops!對不起我的錯誤,它的if(indexPath.row == 3)但我粘貼在這裏是錯誤:) – Ketan