我向我的UIViewController添加了一個表格視圖。然後我在表格視圖中添加了一個標籤到我的自定義單元格中。我試圖通過使用標籤來更新tableview中的標籤。似乎它不工作。使用UIViewController裏面的tableView標籤來更新標籤
這是我的代碼。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
let object = lineItemsObject[indexPath.row]
//cell.textLabel?.text = object["quantity"]
print(object["description"]!)
if let descriptionLabel = cell.viewWithTag(500) as? UILabel {
descriptionLabel.text = object["description"]
}
else
{
print("fail")
}
return cell
}
當我調用該函數時,它總是不讀取標籤並打印「失敗」。我也爲我的標籤分配了正確的標籤值。
請幫我解決這個問題。
您的代碼看起來不錯。我用一個測試項目檢查它,它工作正常。 試試這個: 刪除表格視圖附帶的單元格。從對象瀏覽器中拖放一個新的單元格,現在添加一個標籤,設置它的標籤,然後設置單元標識符並嘗試。 – TheAppMentor