使用夫特

问题描述:

表视图创建行标题我有字符串 的阵列在顶部级别的数组:使用夫特

Array = [[November 1 2016], [November 2 2016], [November 3 2016, ..., [Current Date]] 

在阵列[2016年11月1日],它包含一个字符串[timeOne,timeTwo,timeThree。 ..]。类似的字符串发生在数组[2016年11月2日]之后。

目标是创建一个表格,其中有一行显示*数组中显示的日期,例如“2016年11月1日”。然后在这个第一行的单个单元格中显示所有箭头的值(timeOne,timeTwo等)。 例如

Row 1 = November 1 2016 
Row 2 = timeOne 
Row 3 = timeTwo 
Row 4 = timeThree 
Row 5 = November 2 2016 

我有创建的阵列的功能“timeOne,timeTwo,timeThree ...”,然后我可以添加到相应的阵列。 我在数组内创建数组的方法存在的问题是每天都会使用多个timeValues。 我怎样才能快速到每天使用应用程序只记录一次日期,并为这一天创建一个新的数组。 (例如,如果应用程序没有打开11月3日,那么阵列包括11月2日,11月4日。但跳过11月3日。

基本上我后面是一个表,看起来有点像这样,但只有一列。它说:上午7:00,上午8:00等会我timeOne和timeTwo分别

有没有要去这个更好的办法? End goal from UI tableview

,最好的办法是利用的UITableViewsections功能在你的情况下,每个日期将是它自己的部分,随后是行的时间。

通过时间

tableView(tableView: UITableView, viewForHeaderInSection: Int) -> UIView? { 
    let label = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 20)) 
    label.text = dates[section] // Would set to the date in the array. 
} 

然后在cellForRowAtIndexPath迭代,并显示这些:

例如,你可以这样做以下。

如果你需要我详细说明任何事情。

编辑:

一种非常原始的实现看起来有点像这样,很明显,你需要填充的日期和时间数组值。

class Time { 
    var timeString: String! 
    var eventString: String! 
} 

class Date { 
    var dateString: String! 
    var times: [Time]! 
} 

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    var dates = [Date]() 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = UITableViewCell() 
     cell.textLabel?.text = dates[indexPath.section].times[indexPath.row].timeString 
     return cell 
    } 

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

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return dates[section].times.count 
    } 

    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let label = UILabel(frame: CGRectMake(0, 0, tableView.frame.width, 20)) 
     label.text = dates[section].dateString 
     return label 
    } 
} 
+0

谢谢雅各布,我想这就是我所追求的。但是,当我创建每个timeValue时。我也在创建一个日期。我如何在一天内获得多个timeValues,对应于一个部分标题。以及如何使每个部分标题动态更新到下面的值被采取的那一天 – Lucas

+0

请参阅修改后的答案。 –