Button in UITableview changes current cell

I can not find a solution to change my custom cell , because the button is inside. I want to change the text of the shortcut when I click the button.

- (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:nil]; } // View UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)]; [view setBackgroundColor:[UIColor colorWithWhite:255.0 alpha:0.0]]; // Label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320-7-7, 16)]; [label setText:@"Hello"]; [label setTextAlignment:UITextAlignmentLeft]; [label setTextColor:[UIColor grayColor]]; [label setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16]]; [label setBackgroundColor:[UIColor clearColor]]; [view addSubview:label]; // Action UIButton *action = [[UIButton alloc] initWithFrame:CGRectMake(306-54, 0, 54, 54)]; [action setTitle:@"0" forState:UIControlStateNormal]; [action addTarget:self action:@selector(actionMore:) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:action]; // Add view to cell [cell addSubview:view]; return cell; } 

Edit (add details)

 - (void)actionMore:(id)sender{ UIButton *button = (UIButton *)sender; // Edit current cell, but I do not know how... } 

Thanks.

+4
source share
5 answers

You can get the cell in which the button with the following lines of code was clicked

 UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview]; 

Then use the cell link to change the label text

cell.yourlabel.text = @ "New text";

Update I think the above code may not work in iOS 7. The best way is

 CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.mainTable]; NSIndexPath *indexPath = [self.mainTable indexPathForRowAtPoint:buttonPosition]; 
+4
source

To help you provide more details.

  • Is it a single cell or is it a prototype for several?
  • Code in actionMore: selector.
  • What are you trying to do this? Why does a button in a cell change label text?

If this cell is used more than once, the problem may be more efficient: it cannot highlight which cell is the target for the code. You may also have the same typo in the actionMore method, but we cannot solve this without being able to see all the interacting code.

If you can provide more details, someone can help. I will edit this answer if I can help you when I have more information.

- EDIT -

First, to get a cell, you can use:

 UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview]; 

Then you can access your cell label:

 cell.label.text = @"Some Text Here"; 

Your next problem is to find out where the list your cell is in. To do this, use this code:

 UITableView * table = (UITableView*)[[[sender superview] superview] superview]; NSIndexPath * indexPath = [table indexPathForCell: cell]; 

You can then use the switch statement or if-else statements to find which line was run.

+1
source

Since your UIButton and UILabel are subzones of the same view, you can mark your label and use viewWithTag to find it from your button action code:

 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320-7-7, 16)]; label.tag = 1234; .... - (void)actionMore:(id)sender{ UIButton *button = (UIButton *)sender; UILabel *label = [[sender superview] viewWithTag:1234]; // Edit current cell } 
+1
source

better add view as subview in contentview

pass indexpath to actionMore or select it there. after you got the indexpath , you are in business.

you can set a tag for your textlabel to retrieve it from superview .

0
source
 - (void)actionMore:(id)sender{ UITableViewCell * cell = (UITableViewCell*)[[sender superview] superview]; UIView *view = [cell.subviews objectAtIndex:0]; UILabel *label = [view.subviews objectAtIndex:0]; [label setText:@"test"]; } 
0
source

All Articles