2016-08-04 80 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。