Ios 以编程方式创建静态表视图/单元格
我有一个UITableView,作为示例,它包含基于数组内容创建的动态单元格 我可以使用数组计数和indexPath填充这些元素,以呈现每个项目的单元格。我对此很满意,而且效果很好 我现在想尝试以编程方式创建静态单元格 然而,我马上就被难住了,我该如何创造呢?我目前正在重写Ios 以编程方式创建静态表视图/单元格,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个UITableView,作为示例,它包含基于数组内容创建的动态单元格 我可以使用数组计数和indexPath填充这些元素,以呈现每个项目的单元格。我对此很满意,而且效果很好 我现在想尝试以编程方式创建静态单元格 然而,我马上就被难住了,我该如何创造呢?我目前正在重写numberOfRowsInSection和cellforrowthindexpath,如下所示: override func tableView(_ tableView: UITableView, numberOfRowsI
numberOfRowsInSection
和cellforrowthindexpath
,如下所示:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! ProfileCell
cell.rowContent.text = items[indexPath.row]
return cell
}
我怀疑我的第一个错误是
dequeueReusableCell
,非常感谢您的帮助。如果我理解您的问题,您希望在包含动态单元格的tableView中添加静态单元格
如果是这种情况,您可以硬编码,在此处增加返回值:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count+1
}
在本例中,您只需要添加一个静态单元格
在cellForRowAtIndexPath中,应定义要添加此静态单元格的位置。在下面的示例中,它将是第一个单元格:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
//should change StaticCell to the static cell class you want to use.
let cell = tableView.dequeueReusableCell(withIdentifier: "staticCell", for: indexPath) as! StaticCell
//hardcore attributes here, like cell.rowContent.text = "A text"
return cell
}
let row = indexPath.row-1
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! ProfileCell
cell.rowContent.text = items[row]
return cell
}
它基本上是根据您想要使用的静态单元格的数量来移动项目[行]。
我不确定这是否有效,但这是我的第一个猜测(根据我的经验)。试试这个,告诉我它是否有效:)如果我理解您的问题,您希望在包含动态单元格的tableView中添加静态单元格 如果是这种情况,您可以硬编码,在此处增加返回值:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count+1
}
在本例中,您只需要添加一个静态单元格
在cellForRowAtIndexPath中,应定义要添加此静态单元格的位置。在下面的示例中,它将是第一个单元格:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
//should change StaticCell to the static cell class you want to use.
let cell = tableView.dequeueReusableCell(withIdentifier: "staticCell", for: indexPath) as! StaticCell
//hardcore attributes here, like cell.rowContent.text = "A text"
return cell
}
let row = indexPath.row-1
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! ProfileCell
cell.rowContent.text = items[row]
return cell
}
它基本上是根据您想要使用的静态单元格的数量来移动项目[行]。
我不确定这是否有效,但这是我的第一个猜测(根据我的经验)。试试这个,告诉我它是否有效:)可能与目标C重复。这很快。你的问题的答案在两种语言中是相同的(不可能,也没有真正意义)。无法通过编程方式创建UITableViewCell?什么不合理?我想创建5个单元格,都有不同类型的内容。我不想重复使用手机。它可以通过故事板来完成,所以……“什么是没有意义的?”。。。术语“静态单元”有一个非常具体的含义,即当使用固定设计或
@IBOutlet
引用实例化表视图时,它始终存在。但是,如果您想以编程方式添加一个单元格,您只需动态创建一个单元格。您是想从单元原型创建该单元,还是自己以编程方式构建它,这取决于您自己。但它不是一个“静态单元”。可能的复制品是Objective C。这是Swift。你的问题的答案在两种语言中是相同的(这是不可能的,也没有真正意义)。无法通过编程方式创建UITableViewCell?什么不合理?我想创建5个单元格,都有不同类型的内容。我不想重复使用手机。它可以通过故事板来完成,所以……“什么是没有意义的?”。。。术语“静态单元”有一个非常具体的含义,即当使用固定设计或@IBOutlet
引用实例化表视图时,它始终存在。但是,如果您想以编程方式添加一个单元格,您只需动态创建一个单元格。您是想从单元原型创建该单元,还是自己以编程方式构建它,这取决于您自己。但它不是一个“静态细胞”