ios - 反转UITable行号从大到小(...5,4,3,2,1)

标签 ios arrays swift uitableview

我有一个带有 customCell 的 UITableView,其中我有一个标签,将每行的编号显示为 1,2,3,4...,4 是表格底部的最后一行。一切正常,但我想反转数字而不是 4、3、2、1,其中 4 是顶行,1 是最后一行。

这是我用来将数字添加到 customCell 中的标签的代码。

  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("reusableCell", forIndexPath: indexPath) as! CustomCell

        cell.labelRowNumber!.text = String(indexPath.row + 1)// how to reverse the indexPath.row

        return cell
    }

如果不添加一个数组来跟踪每一行的数量,这是否可能?

仅供引用 - 我已经反转添加到表中的内容,它来自数组 itemList.insert(dataCell, atIndex:0)。我只需要反转数字以匹配内容。

谢谢

最佳答案

你只需要以相反的顺序得到值:

cell.labelRowNumber!.text = String(count - indexPath.row)

其中 count 是您显示的行数(即无论您从 numberOfRowsInSection 返回什么)。大概是 itemList.count

关于ios - 反转UITable行号从大到小(...5,4,3,2,1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35305632/

相关文章:

swift - 如何保证在使用 AlamoFire 的 RequestInterceptor 类时一次只运行一次重试?

ios - 在 Swift 中使用 UIAlertController 的内存泄漏

ios - 对 iOS 移动应用程序编写平台的建议?

javascript - 内联与可观察数组的绑定(bind)

ios - 更新优步移动 SDK 上的访问 token

java - 在Java中对对象数组进行冒泡排序

arrays - MongoDB:更新数组中的数组

swift - 在 Swift Mac App 中定义结构

ios - 从 'UIViewController?' 向下转换为 'UIViewController' 仅解开可选值;您的意思是使用 '!' 吗?

ios - 如何从所有文本字段获取所有值