Swift:如何在动态UITableView中使用静态单元格

时间:2015-07-30 07:49:59

标签: ios swift uitableview

static
dynamic
dynamic
dynamic
..
..
dynamic

我有一个包含动态内容的动态表格列表。在列表的顶部,我需要一个静态单元格。

尝试在静态单元格中添加标签以尝试功能。

我的静态单元格标识符是:firstCell

动态单元格的标识符是:cell

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return 5
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...

    cell.textLabel?.text = "test"


    return cell
}

此代码返回5次" test"在我的动态细胞中。当我尝试添加标签并使用静态单元格创建插座时,会出现错误。

我已经在stackoverflow上搜索和搜索,但没有快速版本的解决方案。怎么办?

编辑提高问题:

实际上我需要两个部分,section1将是静态内容(我将以编程方式创建它。例如将使用uidatepicker,uipicker等等),section2将只使用动态数组(这部分是okey)

3 个答案:

答案 0 :(得分:1)

你尝试过这样的事吗?

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    if (indexPath.row == 1) {
        cell.textLabel?.text = "test"
    } else {
        // Configure the cell...
    }

    return cell
}

答案 1 :(得分:1)

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            var cell: IntroductionCell = self.tableView.dequeueReusableCellWithIdentifier("IntroCell") as! IntroductionCell
            cell.labelCellText.text = textArray[indexPath.row].uppercaseString
            return cell
        } else {
            var cell1: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("IntroCell1") as! UITableViewCell
            return cell1
        }
    }

更改First Cell的单元格标识符。

那就是LOL

答案 2 :(得分:1)

覆盖功能。打电话给超级并改变它。

class TableViewController: UITableViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var rowCount = super.tableView(tableView, numberOfRowsInSection: section)
        if section == 1 {
            rowCount -= 1
        }
        return rowCount
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)
        return cell
    }

}