2013-04-22 24 views
0

我有一個UITableView,它由多個自定義UITableViewCells組成。如何更新didSelectRowAtIndex上的自定義UITableViewCell的屬性?

當表didSelectRowAtIndexPath方法火災,如果indexPath.row等於2,我想在此行的單元格內更新UILabel

我的想法是使用以下幾行;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
if(indexPath.row == 2){ 

    myCustomCellView *cell = [self.essentialsTableView cellForRowAtIndexPath:indexPath]; 
    cell.myUILabel.text = @"my text"; 

    [tableView reloadData]; 

    } 
} 

的這裏的問題是cellForRowAtIndexPath返回類型UITableViewCell,不myCustomCellView的對象。

任何人都可以告訴我如何訪問和更新didSelectRowAtIndexPath方法內的單元格的屬性?

回答

0

定義一個布爾值,說「secondRowIsClicked」。在您的viewWillApperviewDidAppear函數中將其設置爲No。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexPath.row == 2) 
    { 
     secondRowIsClicked = YES; 
     [tableView reloadData]; 
    } 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Your code 
    if (secondRowIsClicked == YES) 
    { 
     cell.myUILabel.text = @"my text"; 
    } 
} 
0

嘗試用波紋管代碼..

myCustomCellView *cell = (myCustomCellView *) [self.essentialsTableView cellForRowAtIndexPath:indexPath]; 

你可以重新加載UITableViewCellindexPath像波紋管..

[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; 
+0

@Scratcha開始哎,如果這個答案對你有用,那麼請接受和upvote答案:) – 2013-04-26 04:51:16

0
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

myCustomCellView *cell = [self.essentialsTableView cellForRowAtIndexPath:indexPath]; 
UILabel *yourLabel=[cell.contentViews.subviews viewWithTag:indexPath.row+1]; 
//do whatever you want with your label; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Your code 
      cell.myUILabel.text = @"my text"; 
      cell.myUILabel.tag=indexpath.row+1; 
} 
0

強制轉換UITableViewCell到您的自定義單元格

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(indexPath.row == 2){ 

     myCustomCellView *cell = (myCustomCellView *)[self.essentialsTableView cellForRowAtIndexPath:indexPath]; 
     cell.myUILabel.text = @"my text"; 

     [tableView reloadData]; 

    } 
} 
0

需要UPADTE的datasoure和Den調用重載的實現代碼如下

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
if(indexPath.row == 2){ 

    //update the array which is used as the data provider in cell for row at index path. 

    [tableView reloadData]; 

    } 
} 
0

myCustomCellViewUITableViewCell一個子類。 所以才typcast細胞

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(indexPath.row == 2){ 

     myCustomCellView *cell = (myCustomCellView *)[self.essentialsTableView cellForRowAtIndexPath:indexPath]; 
     cell.myUILabel.text = @"my text"; 

     [tableView reloadData]; 

     } 
    } 

更多信息: 按照蘋果的編碼指導線沒有用戶類別名稱以小寫字母 而不是myCustomCellView使用MyCustomCellView

相關問題