2016-11-14 21 views
0

表視圖創建行標題我有字符串 的陣列在頂部級別的數組:使用夫特

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

回答

1

,最好的辦法是利用的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

請參閱修改後的答案。 –