arrays - 从最低到最高对表格 View 单元格进行排序

标签 arrays swift uitableview sorting

我有一个表格 View ,其中这些字段填充了表格。 locTextArray 包含以英尺为单位的距离。

如何将单元格按英尺数量从最少到最多排序?

我想我可以对数组进行排序,但这会与所有其他数组不匹配。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
    {
        let cell : MainViewCell! = tableView.dequeueReusableCellWithIdentifier("MainCell") as! MainViewCell

        cell.cellTitle.text = nameArray[indexPath.row]  //title
        cell.cellText.text = textArray[indexPath.row]   // text

        let formatter = NSNumberFormatter()
        formatter.numberStyle = .CurrencyStyle
        cell.userCount.text = formatter.stringFromNumber(Double(priceArray [indexPath.row])!)  //price

        cell.cellDate.text = timeArray[indexPath.row] //time
        cell.cellAddress.text = locTextArray [indexPath.row] //distance in feet

             return cell as MainViewCell
    }

最佳答案

如果您使用对象,一切都会更容易。创建具有属性(名称、文本、价格、时间、locText)的自定义对象。使用其中的一个数组来控制表数据并按任何有意义的属性对其进行排序。

然后你的代码会变成这样:

let obj = objArray[indexPath.row]
cell.cellTitle.text = obj.name
cell.cellText.text = obj.text
// ...

关于arrays - 从最低到最高对表格 View 单元格进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36752584/

相关文章:

用于拆分数据和计算总和的 JavaScript

arrays - Swift - '[Subject] does not have a member named ' 名称' '

ios - 当应用程序被终止/暂停时,iOS 设备可以检测到 Eddystone 信标吗?

swift - 使用 FirebaseUI 删除 Firebase 数据库中的子项

iphone - 选择后自定义 UITableViewCell 背景变为透明

ios - UILabel 在自定义 UItableviewcell 中显示(null)

python - 我的输出格式不正确有人可以帮助我吗?

c++ - 在确定数组的总数和平均值时,如何阻止将哨兵值读入数组?

swift - WKWebview:如何防止 Laravel Page 链接在 safari 中打开?

java - 类型转换 float 组