标签: indexpath

错误:尝试将项目 0 插入第 0 节,但更新后第 0 节中只有 0 个项目

我的代码:

    DataService.dataService.fetchDataFromServer { (channel) in
        self.channels.append(channel)
        let indexPath = IndexPath(item: self.channels.count - 1, section: 0)
        self.collectionView?.insertItems(at: [indexPath])
    }
Run Code Online (Sandbox Code Playgroud)

从服务器获取数据功能:

 func fetchDataFromServer(callBack: @escaping (Channel) -> ()) {
        DataService.dataService.CHANNEL_REF.observe(.childAdded, with: { (snapshot) in
            let channel = Channel(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>)
            callBack(channel)
        })
    }
Run Code Online (Sandbox Code Playgroud)

项目数量部分:

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return 0
}
Run Code Online (Sandbox Code Playgroud)

完整错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert …
Run Code Online (Sandbox Code Playgroud)

ios uicollectionviewcell swift indexpath

1
推荐指数
1
解决办法
1万
查看次数

Swift - 当最后一行不可见时如何对 UITableView 的最后一行执行特定操作?

我试图在 UITableView 的最后一行实现自定义操作。

我找到了一个很好的扩展,以便知道我是否在最后一行:

extension UITableView {

    func isLast(for indexPath: IndexPath) -> Bool {

        let indexOfLastSection = numberOfSections > 0 ? numberOfSections - 1 : 0
        let indexOfLastRowInLastSection = numberOfRows(inSection: indexOfLastSection) - 1

        return indexPath.section == indexOfLastSection && indexPath.row == indexOfLastRowInLastSection
    }
}
Run Code Online (Sandbox Code Playgroud)

我面临的唯一问题是,如果我的 tableView 有很多行且内容可滚动,则最后一行不可见。因此,在我的 cellForRow 中我正在这样做:

if tableView.isLast(for: indexPath) {
   print("Last Row - Do Specific action")
} else {
}
Run Code Online (Sandbox Code Playgroud)

它正在工作,除了假设我的屏幕可以显示 6 行,但我的 tableView 的总行数是 15 。它将在 indexPath 5 和 15 处的行上应用我的特定操作。

我猜测这是因为每次调用 cellForRow 时,它都会检查最后一行,并且它也适用于最后一个可见行。

如何仅在最后一行实现自定义操作,即使它还不可见?

编辑:经过更多调查,问题来自 UITableView …

row uitableview ios swift indexpath

1
推荐指数
1
解决办法
8260
查看次数

Swift超出范围....使用tableView [indexPath]

range出错了......

发生错误cell.creditLabel.text = numOfDays[(indexPath as NSIndexPath).row] + "days".

如果我把tableView.reloadData()cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row]

然后在模拟器中没有任何东西出现......

我该如何解决这个问题..?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return self.nameArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell : MoneyTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MoneyTableViewCell", for: indexPath) as! MoneyTableViewCell


    cell.nameLabel.text = nameArray[(indexPath as NSIndexPath).row]

    cell.creditLabel.text = numOfDays[(indexPath as NSIndexPath).row] + "days"
    cell.levelLabel.text = creditArray[(indexPath as NSIndexPath).row]

    cell.layoutIfNeeded()

    return cell
}
Run Code Online (Sandbox Code Playgroud)

uitableview ios swift indexpath

0
推荐指数
1
解决办法
1204
查看次数

热获取集合视图中现有对象的索引路径

我得到了一个包含对象的数组,并用它们填充集合视图。例如,我的集合视图中有 3 个对象。有没有办法根据对象名称接收当前索引路径?

谢谢

collectionview ios swift3 indexpath

-2
推荐指数
1
解决办法
695
查看次数