0
我有一个NSMutableArray(array2)作为表视图的数据源。当我选择一个searchResultsTableView单元格并重新加载self.tableView与该数组时,我想添加对象到该数组。表格单元格显示swift中NSMutableArray的第一个索引的数据
如果我用array2.addObject()方法添加对象,那么所有的单元格都可以使用单个数据。但是,如果我用array2.insertObject(myObject,atIndex:0)添加对象,则所有单元显示与array2 [0]的数据相同的数据。为什么?
我的问题是在表视图的didSelectRowAtIndexPath函数。我总是想在我的表视图的第一个位置添加选定的对象,这就是为什么我使用insertObject方法而不是addObject方法实现的。以下是我的代码部分。
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.searchDisplayController!.searchResultsTableView {
return self.array1.count
}else{
return self.array2.count
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
if tableView == self.searchDisplayController!.searchResultsTableView {
let number = self.array1[indexPath.row]
cell.textLabel?.text = String(number)
} else {
let cell: customCell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as customCell
let brand = self.array2[indexPath.row] as NSString
cell.name.text = brand
cell.comment.text = "100"
}
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if tableView == self.searchDisplayController!.searchResultsTableView {
let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell!
self.array2.insertObject(cell.textLabel!.text!, atIndex: 0)
//self.array2.addObject(cell.textLabel!.text!)
self.searchDisplayController!.setActive(false, animated: true)
self.tableView.reloadData()
}
}
谢谢,它的工作。 – Nuibb 2015-02-10 12:10:57