在UITableViewCell中,不同的单元格上有不同的按钮,当我单击单元格的按钮时,对所有单元格的按钮执行相同的操作,但是当我按下特定单元格的按钮时,我想要的是那个单元应该工作,我知道我需要将它们放在数组中,但是如何?
当我点击按钮时,一个值正在增加,它应该显示在同一个单元格的标签上。
这是代码: -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
SHEDLIST *info1 = [shed_list objectAtIndex:indexPath.row];
cell.textLabel.text=info1.SHED_Name;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
IDlbl = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 50, 50)];
IDlbl.text = info1.SHED_ID;
IDlbl.textAlignment = NSTextAlignmentCenter;
[cell addSubview:IDlbl];
tickbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tickbtn setBackgroundImage:[UIImage imageNamed:@"tick.png"]forState:UIControlStateNormal];
[tickbtn addTarget:self action:@selector(addsheddata) forControlEvents:UIControlEventTouchUpInside];
tickbtn.frame = CGRectMake(170, 5, 30, 30);
[cell addSubview:tickbtn];
return cell;
}
-(void) addsheddata:(UIEvent *)event
{
// here i am performing my action which i want.
}
答案 0 :(得分:1)
首先为您的按钮分配标签。
[tickbtn setTag:indexPath.row];
[tickbtn addTarget:self action:@selector(addsheddata:) forControlEvents:UIControlEventTouchUpInside];
tickbtn.frame = CGRectMake(170, 5, 30, 30);
[cell addSubview:tickbtn];
通过此更正您的方法实现。
-(IBAction) addsheddata:(UIControl *)sender
{
// here i am performing my action which i want.
UIButton *button = (UIButton*)sender;
NSIndexPath *myIP = [NSIndexPath indexPathForRow:sender.tag inSection:0];
//Type cast cell
UITableViewCell *cell = (UITableViewCell*)[mytblView cellForRowAtIndexPath:myIP];
UILabel *lbl = cell.lbl;
lbl.text = @"Your text";
}
答案 1 :(得分:0)
将这样的目标添加到按钮
[tickbtn addTarget:self action:@selector(addsheddata:event:) forControlEvents:UIControlEventTouchUpInside];
方法应该是这样的:
- (void) addsheddata:(id)sender event:(id)event {
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:chatContent];
NSIndexPath *indexPath = [YOUR_TABLE_OBJ indexPathForRowAtPoint: currentTouchPosition];
UITableViewCell *cell = [YOUR_TABLE_OBJ cellForRowAtIndexPath:indexPath];
}
答案 2 :(得分:0)
cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tickbtn.tag =500+indexPath.row;
//Initialise your UILabel *lbl and define
lbl.tag = 200+indexPath.row;
lbl.text = // Add Text
// Add the Uilabel to the tableView
}
处理标签的行动
-(IBAction) addsheddata:(UIButton *)sender{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag-500 inSection:0];
UILabel *lbl = (UILabel *)[self.view viewWithTag:indexPath.Row+200];
NSLog (@"%@", lbl.text);
// Do further as per your Requirement
}
答案 3 :(得分:0)
tickbtn = [UIButton buttonWithType:UIButtonTypeCustom]; [tickbtn setBackgroundImage:[UIImage imageNamed:@“tick.png”] forState:UIControlStateNormal]; [tickbtn addTarget:self action:@selector(addsheddata)forControlEvents:UIControlEventTouchUpInside]; tickbtn.frame = CGRectMake(170,5,30,30);
[cell addSubview:tickbtn];
NSString * str = [urarray objectAtIndex:indexPath.row];
[tickbtn setTag:[str intValue]];
- (void)addsheddata:(UIEvent *)event
{
UITableViewCell * clickedCell =(UITableViewCell *)[发送者超级视图];
NSIndexPath *clickedButtonPath = [urtableview indexPathForCell:clickedCell];
}