gpt4 book ai didi

ios - 指定要在 TableView 中添加单元格的位置

转载 作者:行者123 更新时间:2023-11-28 08:40:58 25 4
gpt4 key购买 nike

我正在制作一个杂货 list ,到目前为止我只有一个包含杂货 list 项目的数组。我想在顶部添加“今天”,在三个杂货项目之后添加“每周”,在最后三个杂货项目之前添加“每月”。

我的问题是,我如何指定从 arrayNum2 中插入单元格的位置,这些单元格将是“今天、每周和每月”的购物 list ?

例如:

  (  Today   )
item
item
item
Weekly
item
item
item
Monthly
item
item
item

这是我的代码

//


import UIKit

class ViewController: UITableViewController{


var groceries = [Grocery]()




override func viewDidLoad() {
super.viewDidLoad()

self.groceries = [Grocery( name: "Lemon"), Grocery( name: "Bread"), Grocery( name: "Milk"),Grocery( name: "Tomato"),Grocery( name: "Pasta"),Grocery( name: "Soup"),Grocery( name: "Coke"),Grocery( name: "Potato"),Grocery( name: "Chips")]

// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.groceries.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
var grocery : Grocery

grocery = groceries[indexPath.row]

cell.textLabel?.text = grocery.name

return cell
}

}

和我的 Grocery.swift

import Foundation

struct Grocery {

let name: String
}

最佳答案

解决这个问题有两种可能的方法

  • 多个部分:您在不同的部分指定了不同的数组,因此您可以在这些部分中将它们分组和单独

您已经发布了那个 Sample 的链接

  • 在特定位置插入行:您可以使用 insertRowsAtIndexPaths(_:withRowAnimation:) 这在 TableView 中由索引路径数组标识的位置插入行,并可选择动画插入。

因此根据前一个数组的长度/根据您的计算,您可以在特定位置插入行

Apple Doc: insertRowsAtIndexPaths
insertRowsAtIndexPaths Sample code

希望对以后的人有帮助

关于ios - 指定要在 TableView 中添加单元格的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com