Reputation: 5
In UITableViewCell there are different buttons on different cells and when I click on the button of the cell the same action is being performed on all the cell's button, but I want that when I press the button of particular cell then the action of that cell should work, I know I need to put them in array, but how?
when I click on the button one value is being incremented and it should be shown on the label of the same cell.
here is the code:-
- (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.
}
Upvotes: 0
Views: 88
Reputation: 15335
At 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
}
Action by handling the tag
-(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
}
Upvotes: 0
Reputation: 35
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 *)[sender superview] ;
NSIndexPath *clickedButtonPath = [urtableview indexPathForCell:clickedCell];
}
Upvotes: 0
Reputation: 4671
Add target like this to your button
[tickbtn addTarget:self action:@selector(addsheddata:event:) forControlEvents:UIControlEventTouchUpInside];
And method should be like this:
- (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];
}
Upvotes: 0
Reputation: 9836
First assign tag to your button.
[tickbtn setTag:indexPath.row];
[tickbtn addTarget:self action:@selector(addsheddata:) forControlEvents:UIControlEventTouchUpInside];
tickbtn.frame = CGRectMake(170, 5, 30, 30);
[cell addSubview:tickbtn];
Correct your method implementation by this.
-(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";
}
Upvotes: 1