我如何在 tableView swift 中使用循环

how can i use loop for in tableView swift

如何在 cellForItemAtIndexPath 中使用 for 循环

这是我的代码,有什么帮助吗?

我希望每个循环有 return 个单元格

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell:CellCollectionView = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CellCollectionView
    for Restaurent1 in Resturent.Restaurants
    {
        var ResturentName = eachRestaurent.name
        var ResturentDescrption = eachRestaurent.descrption
        var ResturentId = eachRestaurent.id

        cell.ResturentsName.text = ResturentName
        cell.ResturentsDescrption.text = ResturentDescrption
        cell.ResturentsId.text = String(ResturentId as! Int)
    }
    return cell
}

不要在 cellForItemAtIndexPath 中使用循环。该循环已内置于 Cocoa 中,它会为需要呈现的每个单元格调用您的 cellForItemAtIndexPath 实现。

此 API 遵循 "pull" 模型,而不是 "push"。 Table 在需要时查看 "pulls" 来自您的代码的数据,而不是您的代码 "pushing" 将所有数据一次性放入 API。这种方法的优点是 "pull" API 不会给您回电超过需要的次数。例如,如果 100 家餐厅中只有四家可见,则您的方法将被调用四次,而不是 100 次。

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell:CellCollectionView = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CellCollectionView
    let r = esturent.Restaurants[indexPath.row]
    cell.ResturentsName.text = r.name
    cell.ResturentsDescrption.text = r.descrption
    cell.ResturentsId.text = String(r.id as! Int)
    return cell
}