2016-08-04 79 views
-1

我目前正在嘗試循環遍歷UITableView的單元格,以刪除相關viewController中包含的特定UILabel。循環遍歷UITableView中的單個UITableViewCells以編輯單元格中的文本

我該怎麼做呢?

上下文:數組specialBool包含6個布爾值。索引位置對應於特殊或不特定的產品。如果布爾值爲true,那麼我希望該特定產品單元格中的標籤(previousPrice)被刪除...如果它爲假,那麼標籤應該保持正常。

這是我的代碼到目前爲止。這是不完整的:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! exploreTableView 

cell.photo.image = globalVariable.images[indexPath.row] 
cell.name.text = globalVariable.names[indexPath.row] 
cell.price.text = globalVariable.prices[indexPath.row] 
cell.unitPrice.text = globalVariable.unitPrice[indexPath.row] 
cell.pPrice.text = globalVariable.previousPrice[indexPath.row] 

    var i = 0 
    while i < globalVariable.previousPrice.count { 
     if (globalVariable.specialBool[i] == true) { 

      // Strike through previous price 
      let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: globalVariable.previousPrice[i]) 
      attributeString.addAttribute(NSStrikethroughStyleAttributeName, value: 2, range: NSMakeRange(0, attributeString.length)) 
      cell.pPrice.attributedText = attributeString; 

     } else { 
      cell.pPrice.text = globalVariable.previousPrice[i] 

     } 
     i += 1 
    } 

    return cell 
} 
+0

請將代碼的相關部分作爲文本添加到您的問題中,而不是圖片。 – JAL

回答

1

你應該先了解你在做什麼,你想做的事。深入瞭解UITableView

您不需要while循環和cellForRowAtIndexPath調用每個可見單元格(這些單元格可用於其他索引路徑)。

//Remove while loop and var i = 0 

//This will call for each visible cell so replace `i` with indexPath.row 
if globalVariable.specialBool[indexPath.row] == true { 
// do your work here 
} 
0

你爲什麼要循環遍歷每個單元格?在你的tableview委託方法中:cellForRowAtIndexPath:,實現你的代碼來管理標籤。

BOOL boolVal = [array objectAtIndex:indexPath.row]; 
if (boolVal) { 
    //strike out previous price label. 
}else { 
    //keep the label normal. 
} 

當你永遠通過細胞要循環,只需撥打[tableView reloadData]重新加載所有細胞的UITableView。