Ios UITableViewCell detailTextLabel在滚动时消失
我正在使用一个字符串数组,从中设置Ios UITableViewCell detailTextLabel在滚动时消失,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我正在使用一个字符串数组,从中设置detailTextLabel。最初所有字幕都设置正确,但如果我滚动detailtextlab就会消失 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
detailTextLabel
。最初所有字幕都设置正确,但如果我滚动detailtextlab
就会消失
- (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;
}
我用的是iphone6和ios8。我也在使用故事板,并将
UITableViewCell
样式设置为Subtitle
好的,现在我们已经发现了问题(人身上的电话号码文本为零),您可以通过几种方法来解决它
似乎您不想将文本设置为空白。我想这是因为它以一种奇怪的方式布置了单元格,标题被推到了顶部,但下面什么都没有。可以理解
因此,您可以创建一个自定义的UITableViewCell
子类。在其中,你可以自己管理布局,如果号码为零,则以一种方式进行布局,如果有电话号码,则以另一种方式进行布局
更简单的方法是使用两个不同的原型单元
在情节提要中,创建两个原型单元
一个类型为Basic
,并给它一个reuseIdentifiernoPhoneNumberCell
。
另一个具有类型Subtitle
和重用标识符phoneNumberCell
然后在代码中你可以这样做
- (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;
}
现在将创建两个单元格队列。一个给有电话号码的人,一个给没有电话号码的人
这样,您就不会混淆两者,从而避免您面临的问题。如果您记录字符串
person.phone
它是否总是具有正确的值?另外,您是否正确设置了单元格类型?(在interface builder中?)是的,奇怪的是字符串具有正确的值,并且不是空的或零。我想这可能是iOS 8特有的bug。在IB中将单元格类型设置为Subtitle,这应该是正确的,因为在我开始滚动之前,结果很好。是否有其他方法,如tableView:willDisplayCell
?另外,它只是一个标准的UITableViewCell
,还是您正在使用一个子类?如果有,里面有代码吗?它是标准的UITableViewCell
。我只使用了scrollViewWillBeginDragging:
来关闭键盘,但这不会影响单元格。@RoOmin如果这是问题所在,那么当尝试将单元格出列时,应用程序将崩溃。这不是问题。你太棒了。非常感谢你的回答。我只是稍微改进了一下,因为字符串可能不是零而是空的。因此,我将if语句更改为if(person.phone&![person.phone IsequalString:@”“])
再次非常感谢!杰出的很高兴你让它工作了。这个改变也是个好主意。请给你的答案添加一个解释
[cell.detailTextLabel sizeToFit];