Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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/xcode/7.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
Swift CollectionViewCells问题_Swift_Xcode_Uicollectionview_Uicollectionviewcell_Nsindexpath - Fatal编程技术网

Swift CollectionViewCells问题

Swift CollectionViewCells问题,swift,xcode,uicollectionview,uicollectionviewcell,nsindexpath,Swift,Xcode,Uicollectionview,Uicollectionviewcell,Nsindexpath,很抱歉这个模糊的标题,但我不完全确定该怎么称呼它。我在collectionview中有一个集合视图单元格列表,这些单元格只有白色背景。我总共有20个单元格,我希望第一个单元格有青色背景,第四个单元格有绿色背景。我的问题是,如果列表足够大,我滚动的颜色似乎是随机的,有时顶部有4个绿色和2个青色,而不是只有1个青色和1个绿色。我认为这是由于在func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:inde

很抱歉这个模糊的标题,但我不完全确定该怎么称呼它。我在collectionview中有一个集合视图单元格列表,这些单元格只有白色背景。我总共有20个单元格,我希望第一个单元格有青色背景,第四个单元格有绿色背景。我的问题是,如果列表足够大,我滚动的颜色似乎是随机的,有时顶部有4个绿色和2个青色,而不是只有1个青色和1个绿色。我认为这是由于在func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell方法中使用index path.row,并基于indexPath.row分配颜色。我认为index path.row会随着我的滚动而变化,因此当我滚动到底部index path.row时,它是屏幕顶部的项目,而不是列表顶部的项目。我知道这不是实现这一点的正确方法,是否有办法从列表中获取第一个/最后一个项目,而不是当前屏幕上的第一个/最后一个项目?有没有更好的办法完全解决这个问题

下面是一个问题的简单示例-

编辑:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int` is return 20 and in `func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell` this is what I have - `let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Invite Cell", for: indexPath) as! InviteCell
    print(indexPath.row)

    if indexPath.row == 0 {
        cell.InviteCellContainer.backgroundColor = UIColor.cyan
    } else if indexPath.row == 5 {
        cell.InviteCellContainer.backgroundColor = UIColor.green
    }
    return cell
}

假设您的代码没有错误(我无法判断,因为您没有包含任何错误),那么您应该在每个cellForItemAt之后调用
collectionView.reloadData()
。让我知道当你这样做时会发生什么。

假设你的代码没有错误,我无法判断,因为你没有包含任何错误,看起来你应该在每个cellForItemAt之后调用
collectionView.reloadData()
。请告诉我执行此操作时会发生什么情况。

单元格将被重复使用。确保任何UI元素在
cellForItemAt:

在代码中,如果行不是0而不是5,则状态是未定义的。因此,您需要为所有其他索引添加一个案例:

if indexPath.row == 0 {
    cell.InviteCellContainer.backgroundColor = UIColor.cyan
} else if indexPath.row == 5 {
    cell.InviteCellContainer.backgroundColor = UIColor.green
} else {
    cell.InviteCellContainer.backgroundColor = UIColor.gray // or what the default color is
}
return cell
更具描述性的语法是
开关
表达式

switch indexPath.row {
    case 0: cell.InviteCellContainer.backgroundColor = UIColor.cyan
    case 4: cell.InviteCellContainer.backgroundColor = UIColor.green
    default: cell.InviteCellContainer.backgroundColor = UIColor.gray
}

细胞被重复使用。确保任何UI元素在
cellForItemAt:

在代码中,如果行不是0而不是5,则状态是未定义的。因此,您需要为所有其他索引添加一个案例:

if indexPath.row == 0 {
    cell.InviteCellContainer.backgroundColor = UIColor.cyan
} else if indexPath.row == 5 {
    cell.InviteCellContainer.backgroundColor = UIColor.green
} else {
    cell.InviteCellContainer.backgroundColor = UIColor.gray // or what the default color is
}
return cell
更具描述性的语法是
开关
表达式

switch indexPath.row {
    case 0: cell.InviteCellContainer.backgroundColor = UIColor.cyan
    case 4: cell.InviteCellContainer.backgroundColor = UIColor.green
    default: cell.InviteCellContainer.backgroundColor = UIColor.gray
}

您应该设置背景色,而不考虑其位置

if(indexPath.row == 0) {
    cell.InviteCellContainer.backgroundColor = UIColor.cyan
} else if(indexPath.row == 5) {
    cell.InviteCellContainer.backgroundColor = UIColor.green
} else {
    cell.InviteCellContainer.backgroundColor = UIColor.white
}
return cell
这可能是因为您没有在单独的类中定义单元格,并使用函数
prepareforuse()
将背景设置回白色。单元格在collectionView中重复使用,因此有时如果设置数据(而不重置数据),当再次使用单元格时,数据将保持不变。
希望这有帮助

您应该设置背景色,而不考虑其位置

if(indexPath.row == 0) {
    cell.InviteCellContainer.backgroundColor = UIColor.cyan
} else if(indexPath.row == 5) {
    cell.InviteCellContainer.backgroundColor = UIColor.green
} else {
    cell.InviteCellContainer.backgroundColor = UIColor.white
}
return cell
这可能是因为您没有在单独的类中定义单元格,并使用函数
prepareforuse()
将背景设置回白色。单元格在collectionView中重复使用,因此有时如果设置数据(而不重置数据),当再次使用单元格时,数据将保持不变。
希望这有帮助

我添加了问题上两个collectionview函数的代码我添加了问题上两个collectionview函数的代码