如何在tableview Swift中自定义两个不同的部分

时间:2022-05-14 15:40:00

I have successfully implemented two different sections in my table view. Section 1 has 3 cells displayed first, then section 2 has 12 displayed afterwards.

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

I would like to have it ordered so section 1 has its 3 cells mixed into section 2 (12 cells) So in this case it would be displayed every 4 cells. I would like to code it in a way that when the section 2 cells increases over time it keeps section 1 every 4 cells.

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

Here is the tableview delegate functions I have at the moment

这是我目前的tableview委托函数

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


1  

You cannot mix the cells of different sections.
Since that is what you want the solution is to remove the sections all together or include more sections:

你不能混合不同部分的细胞。既然这就是你想要的解决方案是将所有部分一起删除或包含更多部分:

  • Solution 1: 1 Section with x cells: Cell|Cell|Cell|Cell|MovingCell|Cell|Cell|Cell|Cell|Moving Cell
  • 解决方案1:1 x细胞切片:细胞|细胞|细胞|细胞| MovingCell |细胞|细胞|细胞|细胞|移动细胞

  • Solution 2: y Sections with 1 or z cells - Cell|Cell|Cell|Cell + MovingCell + Cell|Cell|Cell|Cell + Moving Cell
  • 解决方案2:y具有1或z细胞的切片 - 细胞|细胞|细胞|细胞+ MovingCell +细胞|细胞|细胞|细胞+移动细胞

I will show you the code that would be needed for Solution 1. You will need to have some variable indicating what the length of Cell-semi-sections would be, e.g. let cellCountBeforeMoving = 4:

我将向您展示解决方案1所需的代码。您将需要一些变量来指示Cell-semi-sections的长度,例如,让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
    }
}

Note the indexes are now a little offset and you have to be careful accessing the array. The probably correct way to get the element corresponding to a given indexPath is

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

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

Solution 1 will end up something like: (you need some var which gives you the number of interplaced MovingCells)

解决方案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
    }
}

#1


1  

You cannot mix the cells of different sections.
Since that is what you want the solution is to remove the sections all together or include more sections:

你不能混合不同部分的细胞。既然这就是你想要的解决方案是将所有部分一起删除或包含更多部分:

  • Solution 1: 1 Section with x cells: Cell|Cell|Cell|Cell|MovingCell|Cell|Cell|Cell|Cell|Moving Cell
  • 解决方案1:1 x细胞切片:细胞|细胞|细胞|细胞| MovingCell |细胞|细胞|细胞|细胞|移动细胞

  • Solution 2: y Sections with 1 or z cells - Cell|Cell|Cell|Cell + MovingCell + Cell|Cell|Cell|Cell + Moving Cell
  • 解决方案2:y具有1或z细胞的切片 - 细胞|细胞|细胞|细胞+ MovingCell +细胞|细胞|细胞|细胞+移动细胞

I will show you the code that would be needed for Solution 1. You will need to have some variable indicating what the length of Cell-semi-sections would be, e.g. let cellCountBeforeMoving = 4:

我将向您展示解决方案1所需的代码。您将需要一些变量来指示Cell-semi-sections的长度,例如,让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
    }
}

Note the indexes are now a little offset and you have to be careful accessing the array. The probably correct way to get the element corresponding to a given indexPath is

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

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

Solution 1 will end up something like: (you need some var which gives you the number of interplaced MovingCells)

解决方案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
    }
}