ios - 在 Swift 中将按日期分隔的部分添加到 UITableView

标签 ios uitableview date swift sections

我在 Swift 和 iOS 编程方面完全是新手,所以您必须原谅这个也许很简单的问题。

我创建了一个 tableView,它在按下按钮时显示数组(字符串)的内容。 现在,我想将这些字符串“分组”到 tableView 部分,按日期排序。

更详细地说:当用户点击按钮时,字符串应该插入到数组的索引 0 处,并显示在标题为今天的部分中。如果数组中的值早于今天的日期,则这些值应显示在该日期的单独部分中。每个部分应对应一天 24 小时,并显示当天添加的所有字符串。

这是我到目前为止所取得的一些示例代码:

var testArray[String]()
var sectionsInTable[String]()

@IBOutlet weak var testTable: UITableView!

@IBAction func saveButton(sender: AnyObject) {

testArray.insert("\(strTest)", atIndex: 0)
testTable.reloaddata()

}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return sectionsInTable.count

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{

    return testArray.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")

    cell.textLabel.text = String(testArray[indexPath.row])        

    return cell
}

我真的不知道如何管理这些部分。希望有人能指出我正确的方向。谢谢!

最佳答案

我需要类似的东西,而 Ron Fessler的解决方案有效,当有很多部分/行时,表加载数据需要很长时间,即使在那之后它也没有太多响应。我认为那里的主要问题是 getSectionItems 函数,因为它总是会遍历所有项目...

我的解决方案:

struct TableItem {
    let title: String
    let creationDate: NSDate
}

var sections = Dictionary<String, Array<TableItem>>()
var sortedSections = [String]()

@IBAction func saveButton(sender: AnyObject) {

    let date:String = "your date in string..."

    //if we don't have section for particular date, create new one, otherwise we'll just add item to existing section
    if self.sections.indexForKey(date) == nil {
        self.sections[date] = [TableItem(title: name, creationDate: date)]
    }
    else {
        self.sections[date]!.append(TableItem(title: name, creationDate: date))
    } 

    //we are storing our sections in dictionary, so we need to sort it 
    self.sortedSections = self.sections.keys.array.sorted(>)
    self.tableView.reloadData()
}

tableView 数据源方法:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return sections.count
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return sections[sortedSections[section]]!.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCellWithIdentifier("Cell")        

    let tableSection = sections[sortedSections[indexPath.section]]
    let tableItem = tableSection![indexPath.row]

    cell.titleLabel?.text = tableItem.title

    return cell
}

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    return sortedSections[section]
}

关于ios - 在 Swift 中将按日期分隔的部分添加到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26596090/

相关文章:

objective-c - 如何为 removeFromSuperview 设置动画

uitableview - ProMotion 造型表格单元(带茶杯)

ios - 从今天的日期减去周和天时的剩余小时

iphone - tableViewController 中的多线程奇怪延迟

Sqlite DateTime 无法识别 Java 当前日期时间

java - 如何在 Java 中仅从月份和年份生成日期?

ios - 如何在 iPhone 的类似数据网格的表格中显示和输入大量数据

objective-c - 查找文件大小

ios - Xcode 8.0 NSManagedObject 子类模板在哪里

ios - 如何为具有动态高度的表格单元格正确设置自动布局?