2016-01-13 64 views
0

enter image description here更改UIButton的顏色和文字上點擊

我必須要改變這種「關注」按鈕上單擊每個單元格「繼」當我在「下面的」一次點擊,就應該回去「跟隨」。 這怎麼可能?

+0

在UItableView的'didSelectRowAtIndexPath'委託中,將單元格的按鈕文本設置爲'button.setTitleColor(UIColor.redColor(),forState state:.Normal)'並將文本設置爲'setTitle(「Title」 ,對於狀態:。普通)'。並添加一個標誌來保存單元格的狀態。 –

回答

1

創建從單元格到控制器的操作。現在你有參考你的單元格按鈕

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

    } 
+0

暫時它看起來像工作,但滾動向上/向下時失敗,因爲單元格在tableview中重用。我們必須在一個單獨的模型中存儲跟隨/以下狀態。您必須根據模型中存在的值配置單元格。 –

+0

@Akhil Jiji。現在檢查答案 –

+0

重新加載部分代碼給我錯誤 –

0

的問題是什麼時候喲點擊按鈕並更改按鈕的一些屬性,也許你重裝的tableView或重裝電池。該狀態沒有緩存。

按鈕的顏色和文本shold obsver用戶模型的屬性(如:followStatus),單擊該按鈕時,喲應該改變德模型followStatus並重新裝入電池。

謝謝。

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

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

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

當你再次點擊同一個單元格時,你必須檢查按鈕文本。你可以通過設置一個布爾值來做到這一點。即當你選擇一個特定的單元格時,將布爾值設置爲YES,並將按鈕文本設置爲「Follow」,並再次單擊相同的單元格時檢查布爾值是否設置,如果已設置,則可以將文本更改爲「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方法委託方法添加此方法來檢查是否該數組包含所選indexpath添加此方法。

[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..} 

通過這種方式,您可以輕鬆管理單元的索引路徑。不管是否發生衝突......