UITableView with multiple items in each cell

I have a table view in which I am trying to place a button with an image and a label. I want to change the image of the button after clicking.

Here is the code:

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; checkedImg = [UIImage imageNamed:@"buttonUnChecked1.png"]; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; } //Set up the cell... NSString *cellValue = [suggestions objectAtIndex:indexPath.row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; check = [UIButton buttonWithType:UIButtonTypeCustom]; check.frame=CGRectMake(0,35,20,20); [check setImage:checkedImg forState:UIControlStateNormal]; [check addTarget:self action:@selector(checkClicked:) forControlEvents: UIControlEventTouchUpInside]; [cell.contentView addSubview:check]; cellContent = [[UILabel alloc]initWithFrame:CGRectMake(40,32,500,25)]; cellContent.text = cellValue; [cell.contentView addSubview:cellContent]; return cell; } -(void)checkClicked:(UIButton *)b { checkedImg = [UIImage imageNamed:@"buttonChecked1.png"]; [check setImage:checkedImg forState:UIControlStateNormal]; } 

At the same time, the image of the buttons changes, but only the last, and not one click. I know the reason for this, but I do not know how to achieve what I want.

+4
source share
2 answers

The easy answer to your problem is to change your checkClicked: method to this:

 -(void)checkClicked:(UIButton *)b { [b setImage:[UIImage imageNamed:@"buttonChecked1.png"] forState:UIControlStateNormal]; } 

But you should also set your tableView:cellForRowAtIndexPath: method to avoid re-creating the button and to fix some memory problems, such as:

 - (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom]; checkBtn.frame = CGRectMake(0,35,20,20); [checkBtn setImage:[UIImage imageNamed:@"buttonUnChecked1.png"]; forState:UIControlStateNormal]; [checkBtn addTarget:self action:@selector(checkClicked:) forControlEvents: UIControlEventTouchUpInside]; [cell.contentView addSubview:checkBtn]; UILabel *cellContentLbl = [[UILabel alloc]initWithFrame:CGRectMake(40,32,500,25)]; cellContentLbl.tag = 1; [cell.contentView addSubview:cellContentLbl]; [cellContentLbl release]; } //Set up the cell... NSString *cellValue = [suggestions objectAtIndex:indexPath.row]; cellContent = (UILabel *)[cell viewWithTag:1]; cellContent.text = cellValue; return cell; } 
+2
source

A structured way to get the result you are looking for:

Subclass UIView for your table cells (containing the button and label). You create these custom views and set them as a contentView for each cell in the table in cellForRowAtIndexPath .

Each of your custom views listens for its own button. When it was pressed, it switches its state and informs the main viewing manager (via the delegate method) that it has been switched. The main view controller calls reloadData in the appropriate cell to cause a reboot with the correct appearance.

Please note that this approach requires that you specify each of the user views that specify the path that it renders in the table - this way it can inform the delegate method of the main view of the controller - this information is necessary to start the reboot of the corresponding cell.

Btw, I suppose you want to see the state of the buttons in the table when the user has finished editing, and your current approach does not very clearly record the state of the state - you will have to iterate over your buttons or add selected items to a mutable array or something like that.

+3
source

Source: https://habr.com/ru/post/1344623/


All Articles