自定义标签单元格的 didSelectRowAtIndexPath [英] didSelectRowAtIndexPath for Custom label Cell

查看:37
本文介绍了自定义标签单元格的 didSelectRowAtIndexPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using this method to add an UILabel to my UITableView and its working fine, but I also need to create a method for selecting the row and save into an temp 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 但不适用于自定义标签时,它工作正常.

but its not working. It was working fine with when I was using cell.textLabel but not with the custom label.

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

    UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *lbltemp = cell1.textLabel;

    _parent.labelone.text = lbltemp.text;       
}

推荐答案

您可以创建自己的 UITableViewCell 子类以允许您获取对标签的引用(并防止您在细胞).或者你可以使用这样的标签:

You could create your own UITableViewCell subclass to allow you to get the reference to your label (and prevent you from creating multiple labels on the cell). Or you could use tags like this:

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

您的原始代码正在添加一个新标签,但随后试图从默认标签中获取文本...它也总是创建一个新单元格并向其添加一个新标签,这非常浪费.

Your original code was adding a new label but then trying to get the text from the default label... It was also always creating a new cell and adding a new label to it which is quite wasteful.

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

Also, you shouldn't usually store text in the label, you should really to to your tableArr to get the text. The tag approach is more suited to updating the label when you reuse the cell or if you're letting the user edit the text (in a UITextField).

这篇关于自定义标签单元格的 didSelectRowAtIndexPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆