0

我有一个UITableView是由一些自定义的UITableViewCells

当 tabledidSelectRowAtIndexPath方法触发时,如果indexPath.row等于 2,我想UILabel在这一行的单元格中更新 a。

我的想法是按照以下思路使用一些东西;

- (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方法中的单元格属性?

4

6 回答 6

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];

    }
}
于 2013-04-22T11:17:11.327 回答
0

您需要更新数据源并调用重新加载表格视图

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

  }
}
于 2013-04-22T12:19:50.587 回答
0

定义一个 Bool 值,例如“secondRowIsClicked”。viewWillApper在您的orviewDidAppear函数中将其设置为 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";
   }
}
于 2013-04-22T11:10:25.120 回答
0

试试下面的代码..

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

你可以像下面这样重新UITableViewCell加载indexPath..

[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
于 2013-04-22T11:11:54.183 回答
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;
}
于 2013-04-22T11:13:48.020 回答
0

myCustomCellView是 的子类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];

      }
    }

更多信息:根据 Apple 编码指南,用户类名称不以小写字母开头,而不是myCustomCellView使用MyCustomCellView

于 2013-04-23T10:08:25.330 回答