[英]Tableview subtitles on IOS7
我正在嘗試為我的tableview單元格添加一個副標題,但它們不會顯示。 哪里出錯了?
行[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle]
與iOS 7一樣是最新的?
最好的祝福
坦率
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"TestA";
cell.detailTextLabel.text = @"TestB";
return cell;
}
這段代碼:
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
永遠不會執行,因為dequeueReusableCellWithIdentifier: forIndexPath:
保證分配一個新的單元格。
不幸的是, registerClass:forCellReuseIdentifier:
不允許你指定UITableViewCellStyle
。
更改dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath
to simply dequeueReusableCellWithIdentifier:CellIdentifier
。 此方法不保證將返回單元格。*如果不是,則代碼將創建具有所需樣式的新單元格。
* - (如果你正在使用故事板,就像rdelmar指出的那樣,但在這里並非如此。)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = @"Title1";
cell.detailTextLabel.text = @"Subtitle 1";
return cell;
}
我有一個類似的問題,互聯網上的解決方案沒有為我工作。 事實證明我是個白痴。 我會發布我的解決方案只是讓其他人體驗類似的情況。
我假設您正在使用故事板 ,已創建原型並將樣式設置為副標題
在您的故事板文檔大綱中 ,請確保選擇原型單元格並選擇副標題。 見圖:
現在確保標簽字體顏色具有我們在您的背景上可見的性質!
簡單地將UITableViewCell子類化並覆蓋
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
第一行是
[super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
並使用表視圖注冊您的單元類
[tableView registerClass:[YourCellSubclass class] forCellReuseIdentifier:@"YourCellID"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.