UITableViewCells显示在其他类型的单元格上

时间:2016-06-21 04:16:16

标签: ios swift uitableview

我在tableView中有两类单元格。一种是标题卡,显示用户的姓名和照片,下面的标题卡显示用户的5个菜单项。我的配置如下:

obj

这里我从各自的数组加载各种项目名称。 因为indexPath 0已被占用,我的第一张菜单卡不会显示 除非我使用 - 1s

进行下面所有奇怪的重置
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    if indexPath.row == 0{
        let cell = self.menuTable.dequeueReusableCellWithIdentifier("userInfoCard", forIndexPath: indexPath) as! userInfoCard

        //Load data

        return cell
    }else{
        let cell = self.menuTable.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! menuItemCell

我理解为什么第一个项目没有加载,但是这是使所有菜单项出现的正确解决方案吗?

1 个答案:

答案 0 :(得分:1)

使用UITableView部分。通过这个你不需要那个棘手的操作

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

现在在你的cellForRowAtIndexPath方法中:

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

    if indexPath.section == 0{
        let cell = self.menuTable.dequeueReusableCellWithIdentifier("userInfoCard", forIndexPath: indexPath) as! userInfoCard

        //Load data

        return cell
    }else{
        let cell = self.menuTable.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! menuItemCell

        //row starts from 0 with section 1
        // No subtraction needed
        cell.itemName.text = self.itemNames[indexPath.row]
    }
相关问题