Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView单元格重复_Ios_Swift_Uitableview_Custom Cell - Fatal编程技术网

Ios UITableView单元格重复

Ios UITableView单元格重复,ios,swift,uitableview,custom-cell,Ios,Swift,Uitableview,Custom Cell,我创建了一个带有显示数据的自定义单元格的tableview。显示器工作正常。但是单元格是重复的,因此显示的单元格多于数组中的项目 override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // when searchcontroller is active, return the number of search results. If inactive simply return

我创建了一个带有显示数据的自定义单元格的tableview。显示器工作正常。但是单元格是重复的,因此显示的单元格多于数组中的项目

 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
    // when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
        return self.AEDItems.count
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.AEDItems.count
    }

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

        // Check if controller is active. If user is doing a search retrieve the aeds from the search result rather than the AED array.
        let aed = self.AEDItems[indexPath.row]
        cell.addressLabel?.text = aed.owner
        cell.objectLabel?.text = aed.street

        return cell
    }

为什么会这样?我想这与细胞的再利用有关。但项目计数不能解决问题吗?

问题在于此方法:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return self.AEDItems.count
}
此方法要求提供节数。您应该从此方法返回
1

相反,您在
aedeitems
数组中拥有与对象一样多的节。然后在其他方法中,您没有放入任何特定于节的逻辑。所以每个部分都是相同的。但我们实际上只需要一个包含多行的部分

因此,我们实际上只想:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return 1
}
或者,正如@johnpatrickmorgan在评论中阐明的那样,我们可以完全省略此可选协议方法,因为如果
UITableView
数据源
省略了此方法,则它只假设一个节


有关更多阅读,请查看。

问题在于此方法:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return self.AEDItems.count
}
此方法要求提供节数。您应该从此方法返回
1

相反,您在
aedeitems
数组中拥有与对象一样多的节。然后在其他方法中,您没有放入任何特定于节的逻辑。所以每个部分都是相同的。但我们实际上只需要一个包含多行的部分

因此,我们实际上只想:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return 1
}
或者,正如@johnpatrickmorgan在评论中阐明的那样,我们可以完全省略此可选协议方法,因为如果
UITableView
数据源
省略了此方法,则它只假设一个节


要了解更多信息,请查看。

您只需要一节。如果是一个,那么就只做一个。如果您有多个部分和每个部分的数据,则将这些数据用于每个部分

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return 1 
    //return self.AEDItems.count
}
要体验此部分,请使用以下UITableView方法

func tableView( tableView : UITableView,  titleForHeaderInSection section: Int)->String {

return "Section"
}

func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float 
{
    return 20.0
}

我建议您查看本教程

您只需要一节。如果是一个,那么就只做一个。如果您有多个部分和每个部分的数据,则将这些数据用于每个部分

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
// when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
    return 1 
    //return self.AEDItems.count
}
要体验此部分,请使用以下UITableView方法

func tableView( tableView : UITableView,  titleForHeaderInSection section: Int)->String {

return "Section"
}

func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float 
{
    return 20.0
}

我建议您检查本教程

您正在返回节的数组计数,这就是它发生的原因,因此请返回1节

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
    // when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
        return 1
    }

您正在返回节的数组计数,这就是它发生的原因,因此请返回1节

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {    
    // when searchcontroller is active, return the number of search results. If inactive simply return the count of the full AED list.
        return 1
    }

您是否使用调试器来验证是否调用了任何表视图数据源方法?嗯。。我想说它们被使用了,因为我可以看到数据正确地从网络加载到数组中,并且这些数据显示在表视图中。您是否使用了调试器来验证是否调用了任何表视图数据源方法?嗯。。我会说它们被使用了,因为我可以看到数据正确地从网络加载到数组中,并且这些数据显示在表视图中。我刚刚尝试了这个。当我将数字设置为1时,它实际上不起作用。。。因为现在,每个单元格中总是显示相同的数据点。但我不确定这是否是从网络加载数据时的错误。我需要检查一下这个…是的。。。还要非常仔细地注意你正在改变的方法。哦。。现在我明白你所说的小心使用方法的意思了。我把行数改为1。谢谢你@约翰·帕特里克:你是对的,也是错的。我们可以省略这一点,因为如果省略此可选协议方法,表视图本身的默认实现(它不是我们的委托的超类)假定
1
。然而,它与数据源的超类无关。我刚试过这个。当我将数字设置为1时,它实际上不起作用。。。因为现在,每个单元格中总是显示相同的数据点。但我不确定这是否是从网络加载数据时的错误。我需要检查一下这个…是的。。。还要非常仔细地注意你正在改变的方法。哦。。现在我明白你所说的小心使用方法的意思了。我把行数改为1。谢谢你@约翰·帕特里克:你是对的,也是错的。我们可以省略这一点,因为如果省略此可选协议方法,表视图本身的默认实现(它不是我们的委托的超类)假定
1
。然而,它与数据源的超类无关。