可能重复: how to change tableView Cell button on click in iphone 我在表视图单元格中有按钮,使用此功能设置背景图像. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)inde
how to change tableView Cell button on click in iphone
我在表视图单元格中有按钮,使用此功能设置背景图像.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SectionsTableIdentifier]; cell.textLabel.text = self.names[self.keys]; if (cell.accessoryView == nil) { UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"]; UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:buttonUpImage forState:UIControlStateNormal]; [button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted]; [button setTitle:@"Send" forState:UIControlStateNormal]; [button sizeToFit]; [button addTarget:self action:@selector(tappedButton:) forControlEvents:UIControlEventTouchUpInside]; cell.accessoryView = button; } return cell; }
Tapped Button的代码是:
- (void)tappedButton:(UIButton *)sender { NSInteger row = sender.tag; // NSString *character = self.names[self.keys]; /* NSString *key = self.keys[indexPath.row]; NSArray *nameSection = self.names[key]; NSString *character = nameSection[indexPath]; */ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Request Notification." message:[NSString stringWithFormat:@"Friend Request Sent ."] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; /* UIImage *buttonUpImage = [UIImage imageNamed:@"button.png"]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [cell.theSyncButton setImage:buttonUpImage forState:UIControlStateSelected]; [button setBackgroundImage:buttonUpImage forState:UIControlStateNormal]; [button setTitle:@"Sent" forState:UIControlStateNormal]; [button sizeToFit]; */ }
现在我要求的是当用户点击表视图单元格中的“发送”按钮时,动态地将背景颜色和标题更改为“已发送”.如何才能获得此功能?
试试这个,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { .... UIButton *button; if (cell.accessoryView == nil) { ... button = [UIButton buttonWithType:UIButtonTypeCustom]; .... cell.accessoryView = button; } button.tag = indexPath.row; ... }
在tappedButton中:
- (void)tappedButton:(UIButton *)sender { NSInteger row = sender.tag; UIImage *buttonUpImage = [UIImage imageNamed:@"button.png"]; [sender setBackgroundImage:buttonUpImage forState:UIControlStateNormal]; [sender setTitle:@"Sent" forState:UIControlStateNormal]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0]; [self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates]; }