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



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

我想订购它,这样第1部分的3个单元格就可以混合到第2部分(12个单元格)中,所以在这种情况下,它将每4个单元格显示一次。我想用这样一种方式来编码它,即当第2节的单元格随着时间的推移而增加时,它保持每4个单元格中有一节。

以下是我目前拥有的表视图代理函数

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:1包含x个单元格的节:Cell|Cell|Cell|Cell|MovingCell|Cell|Cell|Cell|Cell|Moving Cell
  • 解决方案2:y具有1个或z个单元格的截面-Cell|Cell|Cell|Cell+MovingCell+Cell|Cell|Cell|Cell+Moving Cell

我将向您展示解决方案1所需的代码。您需要有一些变量来指示Cell-半部分的长度,例如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,它可以给你提供中间移动细胞的数量)

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
    }
}

最新更新