简体   繁体   English

如何在ios中单击更改tableView单元格按钮图像

[英]How to change tableView Cell button image on click in ios

Possible duplicates : 可能重复:

how to change tableView Cell button on click in iphone 如何在iphone上点击更改tableView单元格按钮

I have button in table view cell with background images set using this function. 我在表视图单元格中有按钮,使用此功能设置背景图像。

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

Code for Tapped Button is : 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];
   */
}

Now i require is to change background color and title to "sent" dynamically when user click on "Send" button in table view cell .How i can get this functionality ? 现在我要求的是当用户点击表视图单元格中的“发送”按钮时,动态地将背景颜色和标题更改为“已发送”。如何才能获得此功能?

Try this, 试试这个,

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ....
    UIButton *button;
    if (cell.accessoryView == nil) {
        ...
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        ....
        cell.accessoryView = button;
    }
    button.tag = indexPath.row;

    ...
}

in tappedButton: 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];

}

Hi you can do following, 嗨,你可以做到以下,

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

In your IBAction method tappedButton you get passed the button that the user tapped (the "sender" parameter.) 在您的IBAction方法tappedButton中,您传递了用户点击的按钮(“sender”参数。)

Change the background color and title of "sender" 更改“发件人”的背景颜色和标题

Use selected state of button, in 使用选中的按钮状态,在

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

In tappedButton() function set sender to selected using [sender setSelected:YES] tappedButton()函数中使用[sender setSelected:YES]将发件人设置为选中

The main reason for this approch is you can detect state of button using isSelected method. 这个approch的主要原因是你可以使用isSelected方法检测按钮的状态。

 [sender setBackgroundColor:[UIColor whiteColor]];
((UIButton *)sender).titleLabel.text = @"title";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM