如何自定义排序表视图中的两个不同部分 Swift

How to custom order two different sections in a tableview Swift

我已经在 table 视图中成功实现了两个不同的部分。第 1 部分先显示 3 个单元格,然后第 2 部分显示 12 个单元格。

我想订购它,所以第 1 部分的 3 个单元格混合到第 2 部分(12 个单元格)中,所以在这种情况下,它将每 4 个单元格显示一次。我想以这样一种方式对其进行编码,即当第 2 节单元格随时间增加时,它会每 4 个单元格保留第 1 节。

这是我目前拥有的table视图委托函数

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.

    return 2
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (section == 0) {
    return 3 // At the moment I have hard coded it will change it to array.count
    } else {
        return eventNameArray.count
    }
}

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

    if indexPath.section == 0 {
    let cell = tableView.dequeueReusableCellWithIdentifier("MovingCell", forIndexPath: indexPath) as! WhatsOnMovingTableViewCell
     // Do something!
    return cell
    }
    else {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! WhatsOnTableViewCell
    // Do something!
    return cell
    }
}

您不能混合不同部分的单元格。
由于这就是您想要的,解决方案是一起删除这些部分或包括更多部分:

  • 解决方案一: 1 个包含 x 个单元格的部分:Cell|Cell|Cell|Cell|MovingCell|Cell|Cell|Cell|Cell|Moving Cell
  • 解决方案 2:包含 1 或 z 单元格的 y 部分 - Cell|Cell|Cell|Cell + MovingCell + Cell|Cell|Cell|Cell + Moving Cell

我将向您展示解决方案 1 所需的代码。您将需要一些变量来指示 Cell-semi-sections 的长度,例如let cellCountBeforeMoving = 4:

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 3 + eventNameArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if (indexPath.item + 1) % (cellCountBeforeMoving + 1) == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("MovingCell", forIndexPath: indexPath) as! WhatsOnMovingTableViewCell
         // Do something!
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! WhatsOnTableViewCell
        // Do something!
        return cell
    }
}

请注意索引现在有一点偏移,您必须小心访问数组。获取对应于给定 indexPath 的元素的可能正确方法是

eventNameArray[indexPath.item - ((indexPath.item + 1) / (cellCountBeforeMoving + 1))]

解决方案 1 最终会是这样的:(您需要一些 var 来为您提供插入的 MovingCells 的数量)

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return movingCells * 2
}

 func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section % 2 == 1 {
        return 1
    }
    return cellCountBeforeMoving
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.section % 2 == 0 {
        let cell = tableView.dequeueReusableCellWithIdentifier("MovingCell", forIndexPath: indexPath) as! WhatsOnMovingTableViewCell
         // Do something!
        return cell
    } else {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! WhatsOnTableViewCell
        // Do something!
        return cell
    }
}