对于 UITableView,我使用了 2 个不同的单元格之一。第一个显示内容,第二个是一个简单的标签,表示如果数组为空则没有内容。我还切换了一个分段 Controller ,它控制要使用的阵列。我有两个单元格的类(class)。第二个只有一个用于标签的 IBoutlet,它将根据分段 Controller 进行更改。我的代码如下所示:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = 0
switch(mySegmentedControl.selectedSegmentIndex) {
case 0:
returnValue = accounts.count
break
case 1:
returnValue = user.count
break
case 2:
returnValue = mutual.count
default:
break
}
return returnValue
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ConnectCell", for: indexPath) as! ConnectTableViewCell
let cell2 = tableView.dequeueReusableCell(withIdentifier: "noUsersCell") as! NoUsersTableViewCell
switch(mySegmentedControl.selectedSegmentIndex) {
case 0:
let user = self.accounts[indexPath.row]
cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self
break
case 1:
let user = self.user[indexPath.row]
cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self
break
case 2:
let user = self.mutual[indexPath.row]
cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self
break
default:
break
}
return cell
}
我刚刚添加了“cell2”,因为这是迄今为止的新功能。我相信我必须使用 numberOfRowsInSection 来检查数组是否为空,如果它为空则返回 1,然后在 CellForRowAt 的情况下放置一个条件来检查数组是否为空,然后使用 cell2。然后我可以更改 cell2 的文本。但是,我不知道如何在休息后返回单个 cell2。有什么想法吗?
最佳答案
我会用两个部分来做到这一点。将第 0 节用于实际数据,将第 1 节用于“无内容”单元格。
如果第一部分中没有行,则以下仅显示第二部分(“无内容”)。
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var returnValue = 0
switch (mySegmentedControl.selectedSegmentIndex) {
case 0:
returnValue = accounts.count
case 1:
returnValue = user.count
case 2:
returnValue = mutual.count
default:
break
}
if section == 0 {
return returnValue
} else {
return returnValue == 0 ? 1 : 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ConnectCell", for: indexPath) as! ConnectTableViewCell
switch(mySegmentedControl.selectedSegmentIndex) {
case 0:
let user = self.accounts[indexPath.row]
cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self
case 1:
let user = self.user[indexPath.row]
cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self
case 2:
let user = self.mutual[indexPath.row]
cell.user = user
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.delegate = self
default:
break
}
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "noUsersCell") as! NoUsersTableViewCell
switch(mySegmentedControl.selectedSegmentIndex) {
case 0:
cell.textLabel.text = "No accounts"
case 1:
cell.textLabel.text = "No users"
case 2:
cell.textLabel.text = "No mutual"
default:
break
}
return cell
}
}
关于ios - UITableView 返回不同的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46535061/