2013-02-07 163 views
0

我有一個表格,其中有5個單元格。在每個單元格中都有一個按鈕。我需要在某些條件下動態更改這些按鈕。如何在表格視圖中動態更改按鈕。在表格視圖單元格中動態更改按鈕

+0

你使用故事板嗎?,你使用靜態細胞? – tkanzakic

+0

你的意思是改變什麼?改變行爲?改變看看?更改文字? – moonwave99

+0

@ moonwave99我需要改變行爲。例如,如果它是「電話」,如果我點擊它應該改爲「短信」。一旦通話完成,下次我只能發短信。 – 2vision2

回答

1

你可以做的是編寫一個函數,它將改變按鈕的值,然後你可以調用該函數。在更改按鈕值後,使用下面提到的任何一種方法:

您可以使用:[tableview reloadData];重新加載所有表數據。

OR

您可以使用下面的方法重新加載特定行:

[tableview reloadRowsAtIndexPaths: indexPath withRowAnimation:anyKindOfAnimation];

1

您可以通過-visibleCells查詢所有可見單元格的表視圖,讓你的按鈕引用(假設你有一個UITableViewCell子類的按鈕屬性)並更改它們。

0

首先你需要爲每個按鈕設置標籤。這可以通過

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
//Your code 
cell.Button.tag = indexPath.row; 
[cell.Button addTarget:self action:@selector(buttonClick1:) forControlEvents:UIControlEventTouchUpInside]; 

}

可以做到,那麼你可以從這個方法

- (IBAction)buttonClick1:(id)sender { 

int tagNo =[sender tag]; 
UITableViewCell *cellclicked = [self.tblProfileFollowing cellForRowAtIndexPath:[NSIndexPath indexPathForRow:tagNo inSection:0]]; 
//Now change the action for the cell 
[cellclicked.Button addTarget:self action:@selector(buttonClick2:) forControlEvents:UIControlEventTouchUpInside]; 

//Your Code 

}

您可以按照相同的步驟獲得按鈕 - (IBAction爲) buttonClick2:(id)寄件人

現在你可以跟蹤哪個按鈕被點擊並更改該按鈕的方法。

並聲明你的手機和按鈕

相關問題