[英]Single-row UITableView doesn't show detailTextLabel until scroll
我有一個通過故事板創建的UITableView,其單元格樣式設置為“ Right Detail”。 僅當以下任何條件為true時,這才能正確顯示textLabel和detailTextLabel:
但是,如果我只有一個沒有做任何事情的條目,則只會顯示textLabel。
我的代碼很基本。 我有一個數組,使用制表符分隔的textLabel / detailTextLabel值填充該數組。 然后,在我的tableView:cellForRowAtIndexPath:函數中,我有以下代碼:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"default"];
NSDictionary *itemDict=[scanArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[itemDict valueForKey:@"quantity"];
cell.textLabel.text=[itemDict valueForKey:@"item"];
[cell setNeedsLayout];
[cell setNeedsDisplay];
return cell;
}
每當我對scanArray對象進行更改時,我都會在表視圖上調用reloadData函數。 這顯然是可行的,因為textLabel確實顯示在表格視圖中,但是如上所述,detailTextLabel沒有(最初)。
編輯:請注意,表的內容不是靜態的-用戶將在正常使用過程中添加和刪除項目。 只是問題僅在將第一項添加到列表后立即發生。 一旦用戶將第二個項目添加到列表中(或執行我上面提到的其他任何操作),問題就會消失。
還已經指出,將我的數據存儲為字典數組而不是制表符分隔的文本會更好。 我同意,並將做出改變。
Edit2:響應於用戶單擊按鈕而更新表。 用戶輸入兩個值(項目編號和數量),然后點擊按鈕。 然后運行以下代碼:
NSDictionary *itemDict=[NSDictionary dictionaryWithObjectsAndKeys:
self.ItemCode.text,@"item",
self.Quantity.text,@"quantity",
returnedData,@"id", nil];
[scanArray insertObject:itemDict atIndex:0]; //The most recent item is always the first item in the list.
[self.ItemCode setText:@""]; //clear out the entry fields
[self.Quantity setText:@""];
[self.HistoryTable reloadData]; //reload the table view.
我也在這里對數據進行其他處理(特別是將其傳輸到服務器),但這與tableView無關。
我發現一種可行的解決方案是替換該行:
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"default"];
有:
UITableViewCell *cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"default"];
並從情節提要中刪除原型單元。 但是,我讀到,這不是最佳實踐。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.