滚动时 UITableViewCell detailTextLabel 消失 [英] UITableViewCell detailTextLabel disappears when scrolling

查看:52
本文介绍了滚动时 UITableViewCell detailTextLabel 消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个字符串数组,我从中设置了 detailTextLabel.最初所有字幕都设置正确,但如果我滚动 detailTextLabel 消失.

I'm using an array of strings where I set the detailTextLabel from. Initially all subtitles are set correctly but if I scroll the detailTextLabel disappears.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"personCell"  forIndexPath:indexPath];

    Person *person = [_persons objectAtIndex:indexPath.row];
    cell.textLabel.text = person.name;
    cell.detailTextLabel.text = person.phone;

    // also tried setNeedsLayout but does not help
    [cell setNeedsLayout];

    return cell;
}

我使用的是 iPhone 6 和 iOS 8.我也在使用故事板并将 UITableViewCell 样式设置为 Subtitle.

I'm using an iPhone 6 and iOS 8. I'm also using storyboard and set the UITableViewCell style to Subtitle.

推荐答案

好的,既然我们已经找到了问题(人的电话号码文本为零),您可以通过几种方法解决它.

OK, now that we've found the problem (with the nil phone number text on the person) you could solve it a couple of ways.

看来您不想将文本设置为空白.我想这是因为它以一种奇怪的方式布置了单元格,标题被推到了顶部,但下面没有任何内容.可以理解.

It seems that you don't want to set the text to blank. I imagine this is due to the fact that it lays out the cell in an odd way with the title pushed up to the top but nothing underneath it. Understandable.

因此,您可以创建一个自定义的 UITableViewCell 子类.在其中,您可以自己管理布局,如果号码为零,则以一种方式进行布局,如果有电话号码,则以不同的方式进行布局.

So, you could create a custom UITableViewCell subclass. In it you can manage the layout yourself and if the number is nil lay it out one way and if it has a phone number lay it out a different way.

更简单的方法是使用两个不同的原型单元.

An easier way would be to use two different prototype cells instead.

在故事板中创建两个原型单元格.

In the storyboard create two prototype cells.

类型为 Basic 的一个,并给它一个重用标识符 noPhoneNumberCell.另一个类型为 Subtitle 和重用标识符 phoneNumberCell.

One with type Basic and give it a reuseIdentifier noPhoneNumberCell. The other with type Subtitle and a reuse identifier phoneNumberCell.

然后在代码中你可以做这样的事情...

Then in the code you can do something like this...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{
    Person *person = [_persons objectAtIndex:indexPath.row];
    UITableViewCell *cell;

    if (person.phone) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"phoneNumberCell" forIndexPath:indexPath];

        cell.detailTextLabel.text = person.phone;
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:@"noPhoneNumberCell" forIndexPath:indexPath];
    }

    cell.textLabel.text = person.name;

    return cell;
}

这将创建两个单元队列.一种适用于有电话号码的人,另一种适用于没有电话号码的人.

This will now create two queues of cells. One for people with phone numbers and one for people without.

这样您就不会将两者混为一谈,从而避免您面临的问题.

This way you don't mix the two and so avoid the problem you are facing.

这篇关于滚动时 UITableViewCell detailTextLabel 消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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