2017-01-10 143 views
1

「生成」 的標籤迅速

["06:58"] 
["07:13", "07:38", "07:53"] 
["08:13", "08:38", "08:53"] 
["09:13", "09:33", "09:53"] 

,我想在tableView顯示它在圖片:

enter image description here

每一個元素 - 這是UILabel,我創建class TimeViewCell: UITableViewCell哪裏有屬性var labels = [UILabel](),但我不知道如何從我的函數中推送數據:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "cellTime", for: indexPath) as! TimeViewCell 
     let showHour = self.infoWripper.sorted(by: { $0.hour < $1.hour }) 

     cell.labelTime.text = showHour[indexPath.row].showHour() 

     for data in showHour { 

      print(data.showFullTime()) //here you see my example of data in console 
     } 

     return cell 
    } 
+3

我建議你在uitableviewcell裏面創建collectionView,除非你有特定的最大可能次數。 – Miknash

+0

你有最多的細胞項目? – Larme

+0

@Larme不,我不知道最大的項目可能是什麼(可能是15,但對於每個細胞的計數可能會改變) –

回答

1

我會添加一個UIStackView到你的UITableViewCell。您可以將帶有文本的標籤添加到UIStackView。

我把你的數據包裝到一個數組中。

let tableViewData = [["06:58"], 
    ["07:13", "07:38", "07:53"], 
    ["08:13", "08:38", "08:53"], 
    ["09:13", "09:33", "09:53"]] 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellTime", for: indexPath) as! TimeViewCell 

    cell.configure(data: tableViewData[indexPath.row]) 

    return cell 
} 

然後,在你的TimeViewCell,你必須添加UIStackView IBOulet(或增加它在程序上)和配置方法。

class TimeViewCell: UITableViewCell { 

    @IBOutlet var labelStackView: UIStackView! 

    func configure(data: Array<String>) { 
     for time in data { 
      let timeLabel = UILabel() 
      timeLabel.text = time 
      labelStackView.addArrangedSubview(label) 
     } 
    } 
} 
+0

謝謝!它的工作很棒! –

+0

和一個問題 - 當我滾動我的tableview - 我的數據在每個單元格添加(不刷新) - 可能是任何變種隱藏它? –

+0

不客氣。 – Yannick