【发布时间】: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