![](/img/trans.png)
[英]Single-row UITableView doesn't show detailTextLabel until scroll
[英]detailTextLabel not showing until selecting a row
大家好,我對uitableviewcell有問題,問題出在detailTextLabel上,我在情節提要中設置了正確的細節。 當視圖出現時,將顯示除detailTextLabel的字符串之外的所有內容。 現在到真正的奇怪部分,當我突然選擇一行時,文本出現。
所以這是我的cellAtIndexPathRow:方法的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 || indexPath.section ==2) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lastCell" forIndexPath:indexPath];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:UITableViewAutomaticDimension];
switch (indexPath.section) {
case 0:
_datePicker = [UIDatePicker new];
_datePicker.datePickerMode = UIDatePickerModeTime;
[cell addSubview:_datePicker];
break;
case 2:
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text = NSLocalizedString(@"Delete alarm", nil);
break;
}
return cell;
} else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:UITableViewAutomaticDimension];
cell.detailTextLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:UITableViewAutomaticDimension];
//cell.detailTextLabel.text = @"";
switch (indexPath.row) {
case 0:
cell.textLabel.text = NSLocalizedString(@"Repeat", nil);
cell.detailTextLabel.text = @"Mo. Tu. We.";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 1:
cell.textLabel.text = NSLocalizedString(@"Cycles", nil);
cell.detailTextLabel.text = @"7";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 2:
cell.textLabel.text = NSLocalizedString(@"Description", nil);
cell.detailTextLabel.text = @"Alarm";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 3:
cell.textLabel.text = NSLocalizedString(@"Sound", nil);
cell.detailTextLabel.text = @"Radar";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
break;
case 4:
_snoozeSwitch = [UISwitch new];
[_snoozeSwitch setOn:YES];
[_snoozeSwitch addTarget:self
action:@selector(updateSwitch:)
forControlEvents:UIControlEventValueChanged];
cell.accessoryView = _snoozeSwitch;
cell.textLabel.text = NSLocalizedString(@"Snooze", nil);
cell.detailTextLabel.text = @"";
break;
default:
NSLog(@"%s:BUG!",__PRETTY_FUNCTION__);
}
return cell;
}
}
所以我希望您的幫助謝謝大家
在將detailTextLabel
更改為空字符串后,是否遇到了iOS 8問題:
cell.detailTextLabel.text = @"";
然后,在稍后將文本更改為其他非空字符串,則該字符串不會出現。 如果您打印detailTextLabel
框架,則可以看到detailTextLabel
大小: {0,0}
嘗試[cell setNeedsLayout];
設置文本后。
在這種情況下可以使用。
有用的鏈接
#編輯
我在iOS 8.1 [cell setNeedsLayout];
沒有用
嘗試[cell layoutIfNeeded];
代替
我遇到了同樣的問題,我能找出的最佳解決方案是將文本設置為@“”而不是@“”。 [cell setNeedsLayout] [cell layoutIfNeeded]和/或[reloadRowsAtIndexPaths ..]對我都不起作用。
我們看不到造成這種行為的代碼,但是我們可以嘗試了解它們的意圖。 我敢打賭他們不會初始化標簽,因為他們不想為空的UILabel使用資源。 畢竟detailTextLabel是可選的,因此它可能不存在。
在創建單元格時,Apple會自動將detailTextLabel.text設置為“ Subtitle”(在界面生成器中)。 因此,不要將文本更改為“”,而是將默認的“字幕”設置為隱藏。
cell.detailTextLabel?.hidden = true
由於我們不知道有一個空標簽時到底發生了什么,因此最好不要將其設置為“”。 尤其是在重復使用細胞的情況下。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.