問題
對于初學(xué)者來說,寫tableView的時候有沒有遇到過在這樣的問題:TableView的cell的detailTextLabel不顯示的問題。
關(guān)于這兩個屬性
detailTextLabel系統(tǒng)的TableView有這個兩個屬性,一個textLabel,一個detailTextLabel,如下官方文檔:
// default is nil. label will be created if necessary.
@property (nonatomic, readonly, strong, nullable) UILabel *textLabel NS_AVAILABLE_IOS(3_0);
// default is nil. label will be created if necessary (and the current style supports a detail label).
@property (nonatomic, readonly, strong, nullable) UILabel *detailTextLabel NS_AVAILABLE_IOS(3_0);
問題總結(jié)
文檔上說default is nil,意思是使用默認(rèn)的cell的時候,這兩個label是空的?還是說這個cell在不用的時候是nil,用的時候會自己創(chuàng)建,這個比較模棱兩可。
如果你要使用系統(tǒng)的cell實現(xiàn)textLabel和detailTextLabel顯示的效果的時候,創(chuàng)建cell的時候?qū)ell的類型指定為UITableViewCellStyleDefault,這時候你會發(fā)現(xiàn),textLabel能顯示出來,而detailTextLabel是顯示不出來的。所以說default is nil?
所以如果要實現(xiàn)這種效果你需要將cell的類型指定為UITableViewCellStyleSubtitle類型。
// Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
UITableViewCellStyleSubtitle
使用UITableViewCellStyleSubtitle類型的cell就能顯示出來detailTextLabel的內(nèi)容了,但是Used in iPod是什么鬼?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellId = @"AddressSelectedController_cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
DDSearchPoi *poi = self.dataSource[indexPath.row];
cell.textLabel.text = poi.name;
cell.detailTextLabel.text = poi.address;
return cell;
}