简体   繁体   English

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

[英]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 有两种方法可以解决这个问题

  • Multiple sections: You specify different array in the different sections, so you would have them group and individual in the sections 多个部分:您在不同的部分中指定了不同的数组,因此您可以在部分中使用它们分组和个体

You have already posted a link of that Sample 您已经发布了该示例的链接

  • Insert row at specific locations: You can use 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.

相关问题 设置添加到表格视图中单元格的图像的大小 - setting the size of the image added to cell in tableview Swift:每次重新加载时,一次又一次添加tableview单元格内容? - Swift: tableview cell content is added again and again with each reload? 在运行时添加文本时,UI Tableview单元格未展开 - UI Tableview cell not expanding when text is added at the runtime 添加到表格单元格的按钮也出现在其他随机单元格上 - Button added to tableview cell appears on other random cells as well 具有一个tableview的分段控件。如何删除在tableview中手动添加的“加载更多”单元格? - Segmented Control with one tableview.How to remove the “Load More” cell which i added manually in the tableview? 在每个单元格都包含表格视图的表格视图中选择新单元格时,无法取消选择先前选择的单元格 - Can’t deselect previously selected cell when a new one is selected in a tableview where each cell contains tableview 当我单击没有元素的Swift时,Tableview单元格只会显示 - Tableview cell only segues when I click where there are no elements Swift swift的自定义tableview单元格中未显示的数据已在下面添加了单元格类 - Data not showing in custom tableview cell in swift have added the cell class below 如何向添加到 tableview 单元格的减号图像添加可访问性标签? - How do i add an accessibility label to the minus image that gets added to the tableview cell? Firebase 实时数据库 - 如果添加了新数据,表视图会更新所有单元格数据 - Firebase Realtime database - tableview update all the cell data if new data added
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM