簡體   English   中英

在UITableView中單擊更改UIButton顏色和文本

[英]Change UIButton colour and text on click in a UITableView

在此處輸入圖片說明

單擊每個單元格時,我必須將“關注”按鈕更改為“關注”,當我再次單擊“關注”時,它應該回到“關注”。 這怎么可能?

創建從單元到控制器的動作。 現在您已經引用了單元格按鈕

-(void)followButtonActionFromTableViewCell:(UIButton *)sender
{  
 [self followUnfolloWithButton:sender]
}

-(void)followUnfolloWithButton:(UIButton *)sender{
//you may need to call web service and set button accordingly

   [sender setTitle:@"Follow" forState:UIControlStateNormal];
    [sender setBackgroundColor:[UIColor yellowColor]];//set your colour

}

如果要重新加載tableview,請使用。

  UIButton * button = (UIButton*)sender;
  CGRect frameRect ;
   NSIndexPath * indexPath ;
   frameRect=[button convertRect:button.bounds toView:self.tableView];
   indexPath= [self.tableView indexPathForRowAtPoint:frameRect.origin];

//Reload 

  [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationAutomatic];

要在cellForRowAtIndexPath方法中保存跟隨/跟隨放置檢查條件的狀態。

通過sender.tag更新模型

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//...your code
cell.yourFollowButton.tag=indexPath.row;

//check condition and set button text and colour 

    }

問題是當您單擊按鈕並更改按鈕的某些屬性時,也許您重新加載了tableView或重新加載了單元格。 狀態未緩存。

按鈕的顏色和文本保留了用戶模型的屬性(例如:followStatus),當單擊按鈕時,應更改模型的followStatus並重新加載單元格。

謝謝。

- (IBAction)btnStartOnClick:(id)sender {

   UIButton *someButton = (UIButton*)sender;
   NSString *str=[someButton titleForState:UIControlStateNormal];

  if ([str isEqualToString:@"Follow"]) 
   {
    [btn setTitle:@"Following" forState:UIControlStateNormal];
   }
  else if ([str isEqualToString:@"Following"])
  {
   [btn setTitle:@"Follow" forState:UIControlStateNormal];
  }

我猜這很簡單。 在您的tableview didSelectRowAtIndexPath委托方法中,您只需要像這樣設置按鈕的顏色和標題。

   [buttonName setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
   [buttonName setTitle:@"Following" forState:UIControlStateNormal];

當您再次單擊同一單元格時,必須檢查按鈕文本。 您可以通過設置布爾值來實現。 例如,當您選擇一個特定的單元格時,將布爾值設置為YES並將按鈕文本設置為“ Following”,然后再次單擊同一單元格,請檢查是否設置了布爾值,如果設置了布爾值,則可以將文本更改為“ Follow”。 另一個選擇是您必須將選定的索引路徑存儲在可變數組中,並且在每次選擇單元格時都嘗試將對象推入並彈出到數組中。這是另一個步驟

首先在您的.h文件中創建一個Mutablearray

@property (strong,nonatomic) NSMutableArray *selectedIndexPaths;

在您的viewDidLoad方法中分配它。

 self.selectedIndexPaths = [[NSMutableArray alloc]init];

在您的.m文件中添加此方法

- (void)addOrRemoveSelectedIndexPath:(NSIndexPath *)indexPath
{
  BOOL containsIndexPath = [self.selectedIndexPaths containsObject:indexPath];

  if (containsIndexPath) {
    [self.selectedIndexPaths removeObject:indexPath];
  }else{
    [self.selectedIndexPaths addObject:indexPath];
  }}

現在,在您的didSelectRowAtIndexPath委托方法中,添加此方法以檢查該數組是否包含您選擇的索引路徑。

[self addOrRemoveSelectedIndexPath:indexPath]//call of the method;
 BOOL isSelected = [self.selectedIndexPaths containsObject:indexPath];

 if(isSelected)
 {
  //change your button text and color.
 }else{//unset your button text and color..}

這樣,您可以輕松管理單元的索引路徑。 不管沖突如何

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM