Uitableview 表格单元格显示第一个索引';swift中NSMutableArray的s数据

Uitableview 表格单元格显示第一个索引';swift中NSMutableArray的s数据,uitableview,swift,didselectrowatindexpath,Uitableview,Swift,Didselectrowatindexpath,我有一个NSMutableArray(array2)作为表视图的数据源。当我选择searchResultsTableView的一个单元格并用该数组重新加载self.tableView时,我想向该数组添加对象 如果我使用array2.addObject()方法添加对象,那么所有单元格都可以处理单个数据 但是,如果我使用array2.insertObject(myObject,atIndex:0)添加对象,那么所有单元格显示的数据都与array2[0]的数据相同。为什么? 我的问题是在表视图的did

我有一个NSMutableArray(array2)作为表视图的数据源。当我选择searchResultsTableView的一个单元格并用该数组重新加载self.tableView时,我想向该数组添加对象

如果我使用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()
        }
    }

你的cellForRowAtIndexPath方法很奇怪!。。。您总是返回“let cell=UITableViewCell()”而实际上不是您的出列“cell”

将您的方法更改为:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if tableView == self.searchDisplayController!.searchResultsTableView {
        let number = self.array1[indexPath.row]
        let cell = UITableViewCell()
        cell.textLabel?.text = String(number)
        return cell
    } 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
    }
}