swift创建了部分uitableview

时间:2015-05-05 14:26:02

标签: uitableview sections

我是swift和面向对象编程的新手,

我正在尝试在我的UI表格视图中的部分中显示一些日期

我想手动修改这些部分的标题

这是我的代码:

    let section1 =
   ["this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",]
let section2 =
   ["this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",
    "this is used tot test the lenght of the text",]

然后我加载它的方法

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 5
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return self.section1.count;
}

func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"cell")
    cell.textLabel!.text = section1[indexPath.row]
    cell.textLabel!.numberOfLines = 3

    return cell
}

我为这些创建了多个常量,但它们不需要分开(它们是常数,它们不会改变)

我的数据只需要5个不同的部分

非常感谢

1 个答案:

答案 0 :(得分:3)

而不是索引路径中的行的单元格实现此委托方法:

override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  if section == 0 {
     tableView.headerViewForSection(section)?.textLabel.text = section1[section]
  } else if section == 1 {
     //and so on for the number of sections you need. 5 or what evet.
  }

}