SWIFT: 创建不同原型样式的tableView

SWIFT: Creating a tableView with different prototype styles

我正在创建一个活动 RSVP 应用程序,RSVP 屏幕设计如下 -

如图所示,有多个 header(对于每个事件),如果用户家庭中的至少一位客人被邀请参加相应的事件,则会向用户显示这些事件。如果显示特定事件的 header,则客人会在其下方列出,用户可以选择 select(或删除 select)代表是否他们正在参加(或不参加)活动。

在 Stack Overflow 的帮助下,我使用以下逻辑实现了具有多个原型单元格的 tableView -

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

    if indexPath.row == 0 {

        //cell 1 content 
        return cell
    } else if indexPath.row == 1 {
       //cell 2 content 
        return cell
    } else if indexPath.row == 2 {
       //cell 3 content 
        return cell
    } ...

实现我想要的东西时,上面的逻辑有问题。是的,indexPath.row == 0 将等于 "Event 1"。但是,indexPath.row == 2,indexPath.row == 3,...,indexPath.row == x,将填充受邀参加特定活动的宾客人数(或 header 如果没有客人被邀请参加活动,将被压制)。问题是,我将使用什么逻辑来显示 "Event 2" header(即 indexPath.row == x + 1)?

如果有任何具体的教程或解决方案可以指出我,将不胜感激。谢谢。

使用事件 ID 作为键和客人的值数组创建字典。

您的 tableView 将使用部分。

部分的数量将是事件的数量(字典的键数)。

每个部分的行数将是客人数组的大小。

像这样 celForRowAtIndex 会很容易实现。

您需要创建一个包含事件 ID、事件名称和来宾子数组(名为来宾)的字典数组(名为 Sections )。 您必须使用 numberOfSectionsInTableView 函数来设置 Header 计数。

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
      // Return the number of sections.
    return self.sections.count
}

将特定部分的单元格数设置为

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

您可以为自定义 HeaderView 在 StoryBoard 中创建一个 TableViewCell 并从

使用它
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let  headerCell = tableView.dequeueReusableCellWithIdentifier("HeaderCell") as! CustomHeaderCell
    headerCell.backgroundColor = UIColor.cyanColor()

      headerCell.headerLabel.text = self.sections[section].eventName

    return headerCell
  }

sub-section 个单元格使用,

 override func tableView(tableView: UITableView,
        cellForRowAtIndexPath indexPath: NSIndexPath)
        -> UITableViewCell {
        let guest = self.sections[indexPath.section].guests[indexPath.row]

        let cell = tableView.dequeueReusableCellWithIdentifier("UITableViewCell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel.text = guest.name
        return cell
    }

阅读此内容以获得更多帮助https://www.ralfebert.de/tutorials/ios-swift-uitableviewcontroller/