我需要在按钮操作中使用touchup来更改tableview单元格中的标签背景颜色。我在表格视图中创建了自定义单元格,然后将20行添加到单元格中(使用NSMutableArray
),然后创建了UIButton
,然后以编程方式实现按钮操作(TouchUPInside操作)。表格单元格更改标签背景颜色的按钮操作
我的条件是“当我点击按钮时,标签背景颜色在特定索引标签(特定行)中变为绿色,但在我的代码中,它反映了所有行中的动作。在所有行中都改变了。“
这里是我的代码:
//Array Declaration
kioskStatus = [NSMutableArray arrayWithObjects:@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Closed",@"Open",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Open",@"Closed",@"Closed",@"Open", nil];
//Table Cell Delegates
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"Cell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; // Custom cell identifier for the string
if (cell == nil) // Check the cell values are nill or not
{
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; // Initialize the customTableviewCell identifer to the cell.
}
cell.kioskStat.text = [kioskStatus objectAtIndex:indexPath.row];
cell.detailsButton.tag=indexPath.row;
**if(cell.detailsButton.selected==YES)
{
cell.kioskStat.textColor=[UIColor greenColor]; //if button is selected the label color to change green
}
else
{
cell.kioskStat.textColor=[UIColor blackColor]; //else the label color to change black
}**
//Button Action
[cell.detailsButton addTarget:self action:@selector(detailsButtonAction:) forControlEvents: UIControlEventTouchUpInside];
return cell; // returns the cell values to the table view.
}
-(void)detailsButtonAction:(UIButton*)sender
{
[kioskStatus objectAtIndex:sender.tag]
NSLog(@"button tapped Index %lu",sender.tag);
return [self.tableView reloadData]; //Reload the table view.
}
这是我的代码。我认为这个按钮的动作是错误的,但我不完全清楚。所以,任何人都可以帮助我解决这个问题。
这是错误假设屏幕截图:
您需要管理特定行中的单击按钮以更改其颜色。 –
在哪里更改标签颜色 – sarosar
@sarosar你想做点像点击按钮? – Mahesh