[英]Specify where the cell will be added in TableView
I am making a Grocery list and so far I only have an array with the grocery list items. 我正在制作一个Grocery列表,到目前为止我只有一个带有购物清单项目的数组。 I want to add " today" at the top, "weekly" after three grocery items and "monthly" before the last three grocery items. 我想在顶部添加“今天”,在三个杂货项目之后添加“每周”,在最后三个杂货项目之前添加“每月”。
My question is, how would i specify where to insert cells from arrayNum2 which will be " today, weekly and monthly" with the grocery list? 我的问题是,我如何指定从arrayNum2插入单元格的位置,这将是“今天,每周和每月”与购物清单?
Ex: 例如:
( Today )
item
item
item
Weekly
item
item
item
Monthly
item
item
item
Here is the code I have 这是我的代码
//
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
}
}
and my Grocery.swift 和我的Grocery.swift
import Foundation
struct Grocery {
let name: String
}
There are two possible ways to solve this problem 有两种方法可以解决这个问题
You have already posted a link of that Sample 您已经发布了该示例的链接
insertRowsAtIndexPaths(_:withRowAnimation:)
which Inserts rows in the table view at the locations identified by an array of index paths, with an option to animate the insertion. 在特定位置插入行:您可以使用insertRowsAtIndexPaths(_:withRowAnimation:)
在表视图中插入由索引路径数组标识的位置的行,并提供动画插入的选项。 So from the length of the previous array/ based on your calculation you can insert the row at specific location 因此,从前一个数组的长度/根据您的计算,您可以在特定位置插入行
Apple Doc: insertRowsAtIndexPaths Apple Doc:insertRowsAtIndexPaths
insertRowsAtIndexPaths Sample code insertRowsAtIndexPaths示例代码
Hope it may help someone in future 希望将来可能对某人有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.