Ios 视图的标记属性的Switch语句
我正在尝试设置一个有2个单元格的tableview。这两个单元都有自己的类,并有自己的方法来配置它们。在代码中,当我使用cellforrowatinexpath方法时,我会陷入困境。我只能将其中一个单元格出列并调用它的方法,这意味着另一个单元格不会被配置。我想配置两者。以下是我(目前)在cellforrow方法中尝试的内容:Ios 视图的标记属性的Switch语句,ios,swift,uitableview,Ios,Swift,Uitableview,我正在尝试设置一个有2个单元格的tableview。这两个单元都有自己的类,并有自己的方法来配置它们。在代码中,当我使用cellforrowatinexpath方法时,我会陷入困境。我只能将其中一个单元格出列并调用它的方法,这意味着另一个单元格不会被配置。我想配置两者。以下是我(目前)在cellforrow方法中尝试的内容: let cells = [tableView.viewWithTag(1), tableView.viewWithTag(2)] for view i
let cells = [tableView.viewWithTag(1), tableView.viewWithTag(2)]
for view in cells {
var reuseIdentifier = "cellID"
switch view?.tag {
case 1: // error occurs here
reuseIdentifier = "storyCell"
let storyCell1 = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! StoryCell1
storyCell1.theCategory.text = newsItem.storyCategory
storyCell1.theTitile.text = newsItem.titleText
storyCell1.firstParagraph.text = newsItem.paragraph1
case 2: // error occurs here too
reuseIdentifier = "storyCell2"
let storyCell2 = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! StoryCell2
storyCell2.configureCell(newsItem)
default:
}
在故事板中,我给了这两个单元格分别为1和2的标签,因此数组在一开始。这种方法有两个问题
因为我确信我已经添加了标记属性,所以我强制展开视图!。标记属性,错误就会消失。因此,第二个问题现在仍然存在 我真的不明白你想在这里做什么 我想您要做的是,在
tableView(u3;:cellforrowatinexpath:)
方法中配置并返回两个单元格。如果你真的想这么做,那你就大错特错了
表视图的数据源方法会提出问题。你的工作就是通过返回一个值来回答这些问题。例如,NumberOfSectionsTableView(:)
询问应该有多少节。示例答案可能是return1
,return10
等
类似地,tableView(\uu:cellforrowatinexpath:)
索引路径指定的节和行中应该显示的单元格是什么
然后返回一个UITableViewCell
。您不能返回两个单元格,因为它要求您提供一个要在该特定节和行中显示的单元格。如果给它两个单元格来显示,表视图如何显示它们?这没有道理!表视图中的每一行只能显示一个单元格
因此,使用indepath
参数来决定要创建哪个单元格,而不是为单元格提供标记
假设您希望第一行显示标识符为“storyCell”的单元格。您希望第二行显示标识符为“storyCell2”的单元格。而您的表视图只有一个部分。您可以这样做:
switch indexPath.row {
case 0:
reuseIdentifier = "storyCell"
let storyCell1 = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! StoryCell1
storyCell1.theCategory.text = newsItem.storyCategory
storyCell1.theTitile.text = newsItem.titleText
storyCell1.firstParagraph.text = newsItem.paragraph1
return storyCell1
case 1:
reuseIdentifier = "storyCell2"
let storyCell2 = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! StoryCell2
storyCell2.configureCell(newsItem)
return storyCell2
default:
// this shouldn't be reached if you do your other data source methods correctly
return UITabeViewCell()
}
你应该删除这些废话:
let cells = [tableView.viewWithTag(1), tableView.viewWithTag(2)]
for view in cells {
var reuseIdentifier = "cellID"
先生,一旦你成为真正的英雄,你就会明白这一点。非常感谢。工作如期进行。