【问题标题】:Swift: How to use static cell in dynamic UITableViewSwift:如何在动态 UITableView 中使用静态单元格
【发布时间】:2015-10-21 11:20:48
【问题描述】:
static
dynamic
dynamic
dynamic
..
..
dynamic

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

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

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

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

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 次“测试”。当我尝试添加标签并使用静态单元格创建插座时,出现错误。

我已经在 stackoverflow 上进行了谷歌搜索,但没有快速版本的解决方案。怎么能这样?

编辑以改进问题:

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

【问题讨论】:

  • 你是什么意思静态?你想添加一个节标题还是你想要实现的目标?

标签: ios swift uitableview


【解决方案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
}

【讨论】:

  • 从逻辑上讲它有效,但是谢谢。如何配置我的手机手册?例如,我的一个静态单元格中需要一个日期选择器。
  • 这是另一个不同的问题,请查看thread。顺便说一句,如果您要将我的答案标记为正确,请不要投票,只需将其标记为正确即可。徽章饿了;P
【解决方案2】:
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
        }
    }

更改第一个单元格的单元格标识符。

就是这样,哈哈

【讨论】:

    【解决方案3】:

    覆盖功能。调用 super 并进行更改。

    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
        }
    
    }
    

    【讨论】:

    • override的定义=“中断动作;通常是为了采取手动控制”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    相关资源
    最近更新 更多