ボタンアクション内でtouchupを使用して、テーブルビューのセルでラベルの背景色を変更する必要があります。私はテーブルビューでカスタムセルを作成し、セルに(NSMutableArray
を使用して)20行を追加してから、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あなたは、Googleのボタンのような何かをしたいですか? – Mahesh