2015-10-18 9 views
1

我有一個2D數組,我想以特定模式在UITableView自定義單元格中填充。iOS Swift:在更新自定義UITableView單元格中的2D數組時獲得重複值

//從parse後臺檢索

var myArray = [["Name1", "Age1"],["Name2", "Age2"],["Name3", "Age3"]] 

//我需要的是:

nameArray = ["Name1", "Name2", "Name3"] 
ageArray = ["Age1", "Age2", "Age3] 

所以,我可以用indexPath填補了自定義的UITableView細胞對於實例名稱的數據: nameArray [indexPath.row]

我嘗試使用用於循環,

var nameArray = NSMutableArray() 
var ageArray = NSMutableArray() 

//Inside CellForRowAtIndexPath 
for data in myArray { 
    self.nameArray.addObject(data[0]) 
    self.ageArray.addObject(data[1]) 
} 

    cell.nameLabel.text = "\(nameArray[indexPath.row])" 
    cell.ageLabel.text = "\(ageArray[indexPath.row])" 

但是我在這兩個單元格中都得到了重複名稱和年齡標籤,並填充了Name1和Age1。有沒有人知道這是什麼錯?

有沒有更好的方法來根據需要重新加載這些數據?

//更新的完全工作代碼感謝@ l00phole誰幫我解決您要添加到nameArrayageArray每次cellForRowAtIndexPath問題

class NewViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet var tableView: UITableView! 
var data = [[String]]() 
var cost = Double() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 
    uploadData() 
} 

func uploadData() { 
    let query = PFQuery(className:"Booking") 
    query.getObjectInBackgroundWithId("X0aRnKMAM2") { 
     (orders: PFObject?, error: NSError?) -> Void in 
     if error == nil && orders != nil { 
      self.data = (orders?.objectForKey("orderDetails"))! as! [[String]] 
      //[["Vicky","21"],["Luke", "18"],["7253.58"]] 
      //*****Removing the last element as it is not needed in the tableView data 
      let count = self.data.count - 1 
      let c = self.data.removeAtIndex(count) 
      cost = Double(c[0])! 
      //****** 
     } else { 
      print(error) 
     } 
     self.reloadTableData() 
    } 
} 

func reloadTableData() 
{ 
    dispatch_async(dispatch_get_main_queue(), { 
     self.tableView.reloadData() 
     return 
    }) 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return data.count 
} 

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

    // Configure the cell... 
    cell.nameLabel.text = "\(data[indexPath.row][0])" 
    cell.ageLabel.text = "\(data[indexPath.row][1])" 
    return cell 
} 
+0

如何提取數據?您不是在索引路徑中調用行內單元格的獲取塊,是嗎? –

+0

您是否在循環之前清除了您的'nameArray'和'ageArray',並向它們添加了新的對象? – t4nhpt

回答

3

被調用,你是不是第一次清除它們。這看起來效率低下,你應該只在輸入數據改變時填充這些數組,而不是在生成單元時。

我甚至不認爲你需要的陣列,你可以只是做:

cell.nameLabel.text = "\(data[indexPath.row][0])" 
cell.ageLabel.text = "\(data[indexPath.row][1])" 
+0

謝謝。你能用例子來解釋更低效的部分嗎?我很懷疑何時重新加載單元格,因爲每當我有大量數據時,我總是會在單元格上傳數據時出現奇怪的行爲。我如何獲得我加載單元格數據時使用動畫一個接一個地插入單元格? –

+0

@VickyArora好的,這聽起來像一個不同的問題,需要從你的相當數量的信息才能正確回答。因此,在解決了當前問題後,開始一個新問題。關於效率低下的一點是,你重複一些沒有效果的東西。名稱/年齡不會一直改變輸入數據不會改變,因此您只有在改變時才需要改變。這可能會對性能產生影響,而且顯然是錯誤的。 – l00phole

+0

@VickyArora關於您剛剛嘗試進行的修改的說明。我無法用我的低級代表來批准它。 2.你在你的問題中聲明輸入數據保存在'myArray'中,而不是'data',所以它不會有很大的意義。 3.這並不重要;重要的是,你明白了;你不需要我的答案中的確切代碼。 – l00phole

1

您不必創建姓名和年齡不同的陣列,可以使用現有的myArray如下

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell:NewTableViewCell = self.tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! NewTableViewCell 
    // Configure the cell... 
    let dataArray = myArray[indexPath.row] 
    cell.nameLabel.text = "\(dataArray[0])" 
    cell.ageLabel.text = "\(dataArray[1])" 
    return cell 
    } 
} 
相關問題