问题描述:

Possible duplicates :

how to change tableView Cell button on click in iphone

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 :

- (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:

- (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.)

Change the background color and title of "sender"

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

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]

The main reason for this approch is you can detect state of button using isSelected method.

相关阅读:
Top