2016-08-04 2 views
-1

現在、関連するviewControllerに含まれる特定のUILabelsを取り除くために、UITableViewのセルをループしています。セル内のテキストを編集するために、UITableView内の個々のUITableViewCellsをループする

どうすればいいですか?

コンテキスト:配列specialBoolには6個のboolが含まれています。インデックスの位置は、特別なものかそうでないものかに対応しています。ブールが真であれば、その特定の製品セルのラベル(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

なぜ各セルをループしていますか?テーブルビューのデリゲートメソッドcellForRowAtIndexPath:の中に、ラベルを管理するコードを実装します。あなたはセルをループにしたいこれまで、ただのUITableViewのすべてのセルをリロードする[tableView reloadData]を呼び出し

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

関連する問題