重新加载集合视图后,如果还选择最后一个单元格,它总是移动到第一个单元格

时间:2018-06-07 10:31:48

标签: ios uitableview uicollectionview swift4

I have attached this image. You can see the problem here我已经实现了水平集合视图,其中单元格包含标签。当我们选择单元格中不可见的项目(在第一次加载4到5项后可见)并重新加载单元格时,它总是移动到第一个可见的单元格。我应该如何处理这个问题?

func collectionView(
        _ collectionView: UICollectionView,
        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier:
    "profile_CollectionViewCell_city", for: indexPath as IndexPath) as!
            profile_CollectionViewCell
    cell.contentView.translatesAutoresizingMaskIntoConstraints = false
    if ((self.data_array[indexPath.row] as! NSMutableDictionary)["title"] != nil) {
        cell.city_name.text = ((self.data_array[indexPath.row] as! NSMutableDictionary)["title"] as? String)?.uppercased()
    }
    if (indexPath.row == selectedIndex) {
        cell.city_name.textColor = UIColor(red: (0.0 / 255.0), green: (120.0 / 255.0), blue: (56.0 / 255.0), alpha: 1)
        cell.city_name.font = UIFont(name: "Poppins-Bold", size: 14)
        cell.lower_view.isHidden = false
    } else {
        cell.city_name.textColor = UIColor(red: (156.0 / 255.0), green: (156.0 / 255.0), blue: (156.0 / 255.0), alpha: 1)
        cell.city_name.font = UIFont(name: "Poppins-Light", size: 14)
        cell.lower_view.isHidden = true
    }

    return cell
}

func collectionView(
        _ collectionView: UICollectionView,
        numberOfItemsInSection section: Int) -> Int {
    return data_array.count
}

func collectionView(
        _ collectionView: UICollectionView,
        layoutCollectionViewLayout: UICollectionViewLayout,
        sizeForItemAtIndexPath: IndexPath) -> CGSize {
    let size = ((self.data_array[indexPath.row] as!
            NSMutableDictionary)["title"] as? String)?.size(withAttributes:
    nil) ?? CGSize.zero
    return size
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAtIndexPath: IndexPath) {
    selectedIndex = indexPath.row
    self.city_table.reloadData()
}

0 个答案:

没有答案