如何将int转换为tableview单元格中的索引路径?

时间:2019-06-19 10:15:15

标签: swift

我必须更改表格视图最后一行的高度。因此,我将数组计数值用作索引路径。

var myArray = NSArray()

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {

        if indexPath.row == myArray.count
        {
            return 400
        }
        return 120
    }

但是高度没有变化。请给我一个解决方案。

2 个答案:

答案 0 :(得分:2)

尝试一下: 数组计数器将从1开始,但您的tableView行将从0开始 因此if indexPath.row == myArray.count永远不会命中

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.row == myArray.count - 1 {
        return 400
    } 
    return 120
}

答案 1 :(得分:1)

if indexPath.row == myArray.count永远不会命中

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    if indexPath.row == myArray.count {
        return 400
    } 
    return 120
}

当dataSource count为myArray.count时,如果您需要更改最后一行的高度,则indexPath.row将从0到myArray.count

if indexPath.row == myArray.count - 1 {

或者不久之后

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
   return ( indexPath.row == myArray.count - 1 ) ? 400 : 120 
}