[英]Why is my UITableViewCell not showing detailTextLabel in ANY style?
這個讓我把頭發拉出來。 我只是想用UITableViewCellStyleSubtitle樣式創建一個帶有文本和細節的表。 但細節並未出現。 通常這是因為有人忘記用正確的樣式初始化單元格,但這不是在這種情況下發生的事情。 我已經嘗試了所有四種單元格樣式,並且細節沒有出現在任何單元格中,盡管當我使用UITableViewCellStyleValue2時文本標簽確實向右移動。
我以前多次成功創建了表格。 在這種情況下,我能想到的唯一重要區別是我沒有使用UITableViewController。 相反,我像任何其他視圖一樣嵌入UITableView,並連接我自己的數據源和委托。
我已經把關鍵方法歸結為:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get the UITableViewCell (out of the recycling pool, or de novo)
NSString *reuseID = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
if (not cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease];
}
cell.textLabel.text = @"Foo";
cell.detailTextLabel.text = @"Bar";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22); // required
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:12]; // also required
//[cell setNeedsLayout]; (makes no difference)
return cell;
}
只有當我包含上面“必需”行的兩個(並使用除Default之外的任何單元格樣式)時,我才能看到詳細文本,即使我這樣做,文本標簽的位置也完全重疊細節標簽的位置。
因此,似乎初始化程序正在創建detailTextLabel UILabel,但將其字體大小設置為零,並將其框架設置為空或屏幕外的內容。 (我已經嘗試在調試器中檢查這些,但它不是很有用 - 字體大小為零,框架為空,表示textLabel和detailTextLabel,但textLabel總是顯示正常。)
顯然,如果必須,我可以通過手動調整標簽大小和字體來解決它。 但是,在這種情況下,我必須這樣做,這通常只是設置樣式,文本和布局是自動的,這讓我非常不安。 我搜索了谷歌和堆棧溢出,但找不到任何類似問題的引用。 有沒有人知道這里發生了什么?
(在iOS 6.1下測試。)
在嘗試了所有這些事情之后,當我在Storyboard中將表格視圖單元格樣式設置為“Subtitle”時,字幕顯示給我
注冊標准的UITableViewCell
類可能會阻止您使用樣式化的UITableViewCell
因為永遠不會執行此代碼:
if (cell == nil) {
// Cell will never be nil if a class/nib is registered
}
解決方案 :您是否在UITableView
注冊了一個筆尖或Class
? 如果您有如下所示的行,請嘗試刪除它:
[self.tableView registerClass:[UITableViewCell class]
forCellReuseIdentifier:"cellReuseID"];
並更新您的出列語句,如下所示:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:"cellReuseID"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:"cellReuseID"];
}
好吧,事實證明我們在這個應用程序上有一個非常不尋常的構建過程,它沒有正確識別應用程序針對Cocoa框架構建的SDK。
因此,Cocoa認為這是一個非常古老的應用程序 - 從iOS4之前的日子開始,在添加detailTextLabel之前。 因此,該框架試圖通過模擬iOS4之前的行為(通過以幾種方式隱藏detailTextLabel)來提供幫助。 在_UIApplicationLinkedOnOrAfter上設置一個斷點,並強制它返回1,允許我破解這個功能並證明它是原因。 (現在我們只需要修復我們的構建過程來報告正確的SDK版本。)
毋庸置疑,這不是大多數人會遇到的問題。 但是為了后人的緣故,我想我會在這里發布答案。
我希望一旦檢查,這將有助於你
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = @"Foo";
cell.detailTextLabel.text = @"Bar";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.frame = CGRectMake(10, 20, 100,22);
return cell;
}
像字體大小,背景顏色更改為cell.labels這些操作,這些所有功能你必須在下面的方法。
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:10];
cell.textLabel.backgroundColor=[UIColor redColor];
cell.textLabel.frame = CGRectMake(10, 20, 100,22);
cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22);
}
如果要在cellForRowAtIndexPath中更改這些類型的屬性,則此方法將丟失這些屬性。
問題是你最有可能注冊你的手機
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
含義
if (not cell) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease];
}
永遠不會被調用,因為單元格永遠不會為空。 因此,表格將始終將單元格初始化為UITableViewCellStyleNone。
解決這個問題的唯一方法是從故事板注冊單元格或者為UITableViewCell創建子類並重寫
(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
方法。
這對我有用:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseIdentifier]) {
}
return self;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.