ฉันมีปุ่มและป้ายกำกับซึ่งเป็นมุมมองย่อยของ UITableView
ค่าป้ายกำกับเริ่มต้นคือ 0
สิ่งที่ฉันต้องการคือ เมื่อฉันคลิกปุ่มบนเซลล์ใดเซลล์หนึ่ง ฉันต้องการเพิ่มค่าในป้ายกำกับเซลล์เดียวกัน (เป็น 1) และแสดงค่านั้นในป้ายกำกับเดียวกัน
และอีกครั้งที่ฉันคลิกปุ่มเซลล์เดียวกัน ป้ายกำกับในเซลล์นั้นควรเพิ่มขึ้น (เป็น 2) และแสดงค่านั้นในเซลล์เดียวกันใน UITableView
รหัสของฉัน..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *Lbl;
UIButton *btn;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Lbl =[[UILabel alloc]init];
[Lbl setFrame:CGRectMake(56, 60, 117, 12)];
[Lbl setBackgroundColor:[UIColor clearColor]];
[Lbl setTextAlignment:NSTextAlignmentLeft];
[Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
Lbl.tag=indexPath.row;
[cell.contentView addSubview:Lbl];
btn =[UIButton buttonWithType:UIButtonTypeCustom];
[btn setTitle:@"add" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
[[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
[btn setFrame:CGRectMake(289, 2, 30, 71)];
btn.tintColor=[UIColor lightGrayColor];
btn.tag=indexPath.row;
[btn addTarget:self action:@selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn];
} else {
Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
btn =(UIButton *)[cell.contentView viewWithTag:indexPath.row];
}
cell.textLabel.text=@"title";
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];
return cell;
}
// button action method
-(void)increaseItemCount:(UIButton *)sender
{
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *path = [tableView indexPathForCell:cell];
NSLog(@"row: %d",path.row);
UILabel *countLbl =(UILabel *)[cell.contentView viewWithTag:path.row];
showItemCount=[countLbl.text intValue] + 1;
NSLog(@"%d",showItemCount);
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];
}
ฉันลองสิ่งนี้ หลังจากคลิกแล้ว ค่าจะแสดงในเซลล์อื่น และเมื่อฉันเลื่อนมุมมองตาราง ค่านั้นจะแสดงในเซลล์ทั้งหมด
ข้อเสนอแนะหรือรหัสใด ๆ