[英]How to change tableView Cell button image on click in ios
可能重復:
我在表視圖單元格中有按鈕,使用此功能設置背景圖像。
- (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;
}
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];
*/
}
現在我要求的是當用戶點擊表視圖單元格中的“發送”按鈕時,動態地將背景顏色和標題更改為“已發送”。如何才能獲得此功能?
試試這個,
- (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];
}
嗨,你可以做到以下,
- (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];
}
在您的IBAction方法tappedButton中,您傳遞了用戶點擊的按鈕(“sender”參數。)
更改“發件人”的背景顏色和標題
使用選中的按鈕狀態,在
- (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]
將發件人設置為選中
這個approch的主要原因是你可以使用isSelected
方法檢測按鈕的狀態。
[sender setBackgroundColor:[UIColor whiteColor]];
((UIButton *)sender).titleLabel.text = @"title";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.