用于自定义标签单元格的didSelectRowAtIndexPath

我正在使用这种方法添加一个UILabel到我的UITableView和它的工作正常,但我也需要创build一个方法来select行并保存到一个临时string。

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)]; [cell.contentView addSubview:label]; label.text = [tableArr objectAtIndex:indexPath.row]; label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0]; return cell; } 

但它不工作。 当我使用cell.textLabel而不是自定义标签时,它工作正常。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath]; UILabel *lbltemp = cell1.textLabel; _parent.labelone.text = lbltemp.text; } 

您可以创build自己的UITableViewCell子类,以便获取对标签的引用(并防止在单元上创build多个标签)。 或者你可以使用这样的标签:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell = nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)]; label.tag = 123123; [cell.contentView addSubview:label]; } UILabel *label = (UILabel *)[cell viewWithTag:123123]; label.text = [tableArr objectAtIndex:indexPath.row]; label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; UILabel *label = (UILabel *)[cell viewWithTag:123123]; _parent.labelone.text = label.text; } 

你原来的代码是添加一个新的标签,但然后试图从默认的标签获取文本…它也总是创build一个新的单元格,并添加一个新的标签,这是非常浪费。

另外,你通常不应该在标签中存储文本,你应该真的去你的tableArr来获取文本。 标记方法更适合于在重用单元格时更新标签,或者让用户编辑文本(在UITextField )。

它不工作的原因是您没有引用didSelectRowAtIndexPath的自定义标签。 获取对自定义标签的引用的最简单方法是使用标签:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; // Also, this is the proper way to use reuseIdentifier (your code is incorrect) UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 300, 30)]; [cell.contentView addSubview:label]; label.text = [tableArr objectAtIndex:indexPath.row]; label.font = [UIFont fontWithName:@"Whitney-Light" size:20.0]; label.tag = 1; return cell; } - (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath { UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath]; UILabel *customLabel = [cell viewWithTag:1]; _parent.labelone.text = customLabel.text; } 

问题是cell1.textLabel不是你创build的标签。 这只是单元格创build的默认标签。

解决scheme可能是:

创build自定义单元格时,将标签添加到您的标签。

label.tag=100;

didSelectRow

使用

 UILabel *lbltemp=(UILabel *)[cell1.contentView viewWithTag:100]; 

那么_parent.labelone.text=lbltemp.text; should _parent.labelone.text=lbltemp.text; should从这个标签上抓取你的文字。

这里([cell.contentView addSubview:label]; _)你正在创build你的自定义标签,并将其作为子视图添加到单元格的内容视图,但在UILabel * lbltemp = cell1.textLabel; 你正试图访问tableview单元格的默认标签。添加一些标识符(如标签)到你的标签,并在didSelectRowAtIndexPath中访问它。

我认为你可以使用默认的标签,并从这个解决方法拒绝只是添加

 cell.textLabel.frame = CGRectMake(10,10, 300, 30); 

在Wain的答案中,内存重用单元格也更好

使用cell.indentationallevel或cell.indentationwidth