自定义标签Cell的didSelectRowAtIndexPath

时间:2013-06-03 17:32:43

标签: ios objective-c uitableview

我正在使用此方法将UILabel添加到我的UITableView并且其工作正常,但我还需要创建一个方法来选择行并保存到临时字符串中。

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

6 个答案:

答案 0 :(得分:10)

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

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

您的原始代码是添加新标签,但后来尝试从默认标签中获取文本...它也始终创建一个新单元格并为其添加新标签,这非常浪费。

此外,您通常不应该在标签中存储文字,您应该到tableArr获取文字。标签方法更适合在重复使用单元格时更新标签,或者如果您让用户编辑文本(在UITextField中)。

答案 1 :(得分:2)

问题是cell1.textLabel不是您创建的标签。它只是单元格创建的默认标签。

解决方案可能是:

创建自定义单元格时,在标签上添加标签。

label.tag=100;

didSelectRow

使用

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

然后_parent.labelone.text=lbltemp.text; should从该标签中抓取您的文字。

答案 2 :(得分:2)

它不起作用的原因是您没有在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;       
}

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我认为您可以使用默认标签并拒绝此解决方法 只需添加

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

在Wain的回答中,重用单元格对记忆力也更好

答案 5 :(得分:0)

使用cell.indentationallevel或cell.indentationwidth

相关问题