ios - 如何在 ios 中单击时更改 tableView Cell 按钮图像

标签 ios objective-c uitableview

可能重复:

how to change tableView Cell button on click in iphone

我在表格 View 单元格中有一个按钮,其背景图像使用此函数设置。

- (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;
}

点击按钮的代码是:

- (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];
   */
}

现在我需要的是在用户单击表格 View 单元格中的“发送”按钮时动态地将背景颜色和标题更改为“已发送”。我如何才能获得此功能?

最佳答案

试试这个,

- (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];

}

关于ios - 如何在 ios 中单击时更改 tableView Cell 按钮图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22960879/

相关文章:

ios - 为什么当 TableView 在其下方滚动时,UIToolBar 会变成透明/半透明

ios - 未调用 cellForItemAtIndexPath,原因不明

ios - 从堆栈为 ViewController 中的 NavigationController 准备 ForSegue

objective-c - Objective-C 中的内联访问器(getter/setter)方法

ios - objective-c : Safe float comparison fails strangely

ios - 如何卡住 View 直到某个值为真?

ios - 导航栏标题字体颜色未使用 RGB 设置,但可以使用标准颜色

ios - Swift 3 TableViewController 不会取消初始化

ios - 自定义 UITableViewCell 图层截断

ios - 如何确保实例没有被释放?