【问题标题】:How to change tableView Cell button image on click in ios如何在ios中单击时更改tableView Cell按钮图像
【发布时间】:2014-04-09 11:17:56
【问题描述】:

可能的重复:

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

点击按钮的代码是:

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

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

【问题讨论】:

  • 使用[sender setBackgroundImage:buttonUpImage forState:UIControlStateNormal]更改tappedButton:中的按钮背景图片
  • [发件人 setImage:buttonUpImage forState:UIControlStateSelected];还是不行
  • 设置图片后尝试通过reloadRowsAtIndexPaths:方法重新加载单元格。

标签: ios objective-c uitableview


【解决方案1】:

试试这个,

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

}

【讨论】:

  • 最后 3 行错误“在控制器类型的对象上找不到属性表视图”:[self.tableView beginUpdates]; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]; [self.tableView endUpdates];
  • self.tableView 指的是 tableView。 (希望 tableView 有一个 IBOutlet 连接),如果是,则将该名称替换为 self.tableView
【解决方案2】:

嗨,你可以做以下,

- (void)tappedButton:(UIButton *)sender
{
    [sender setBackgroundColor:[UIColor grayColor]];
    [sender setTitle:@"Sent" forState:UIControlStateNormal]; 

    UIAlertView *alert = [[UIAlertView alloc]
                      initWithTitle:@"Request Notification."
                      message:[NSString
                               stringWithFormat:@"Friend Request Sent ."]
                      delegate:nil
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil];
     [alert show]; 
 }

【讨论】:

    【解决方案3】:

    在您的 IBAction 方法 tappedButton 中,您获得了用户点击的按钮(“sender”参数)。

    更改“发件人”的背景颜色和标题

    【讨论】:

      【解决方案4】:
       [sender setBackgroundColor:[UIColor whiteColor]];
      ((UIButton *)sender).titleLabel.text = @"title";
      

      【讨论】:

        【解决方案5】:

        使用按钮的选中状态,在

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            [button setTitle:@"Send" forState:UIControlStateNormal];
            [button setBackgroundImage:original_image forState:UIControlStateNormal];
            [button setTitle:@"Sent" forState:UIControlStateSelected];
            [button setBackgroundImage:selected_image forState:UIControlStateSelected];
        }
        

        tappedButton() 函数中,使用[sender setSelected:YES] 将发送者设置为选中

        这种方法的主要原因是您可以使用isSelected 方法检测按钮的状态。

        【讨论】:

        • 图像正在正确更改..但标题没有出现..在点击事件之前和之后..
        • @user3482051 在单元初始化部分和操作函数中使用标题和图像代码,使用 [sender setSelected:YES] 将其设置为活动状态
        • 对不起,我不清楚你指的是哪个动作函数......请在上面的代码中说清楚......
        • 按钮不会出现使用这个:[button setBackgroundImage:buttonUpImage forState:UIControlStateSelected];使用此按钮出现没有标题的按钮:[button setBackgroundImage:buttonUpImage forState:UIControlStateNormal];我怎样才能使标题可见??
        • 您必须为正常和选定状态使用指定标题和图像。我又更新了我的代码,请检查一下。
        猜你喜欢
        • 2014-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        • 1970-01-01
        • 2015-12-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多