Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将2个不同的自定义UITableViewCell调用到UITableViewController中_Ios_Swift_Uitableview - Fatal编程技术网

Ios 将2个不同的自定义UITableViewCell调用到UITableViewController中

Ios 将2个不同的自定义UITableViewCell调用到UITableViewController中,ios,swift,uitableview,Ios,Swift,Uitableview,我一直在网上搜寻,试图找出这个问题,但找不到明确的答案。下面是发生的事情: 我创建了一个名为MainTableViewController的UITableViewController,其中有两个自定义单元格:“FirstCell”和“SecondCell”。它们都使用该命名约定作为重用标识符。我还为每个单元格创建了单元格类文件:“FirstTableViewCell”和“SecondTableViewCell”,并相应地分配给每个单元格。我还为每个UITableViewCell类文件中单元格中的

我一直在网上搜寻,试图找出这个问题,但找不到明确的答案。下面是发生的事情:

我创建了一个名为
MainTableViewController
UITableViewController
,其中有两个自定义单元格:“FirstCell”和“SecondCell”。它们都使用该命名约定作为重用标识符。我还为每个单元格创建了单元格类文件:“FirstTableViewCell”和“SecondTableViewCell”,并相应地分配给每个单元格。我还为每个
UITableViewCell
类文件中单元格中的所有对象创建了
IBOutlets

因此,在
MainTableViewController
中,加载“FirstCell”没有问题。我是这样做的:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell

    cell.friendfirstnameLabel.text = "Michael"
    cell.friendImageView.image = UIImage(named: "mikepic")

    return cell
}
我现在怎么称呼“第二个手机”?我知道我需要将我的报税表改为2。但是,如何更改
dequeueReusableCellWithIdentifier
cellForRowAtIndexPath
代码以使这两个单元格都工作?“第二个单元格”的布局完全不同,定义了不同的对象。

试试这个

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return yourArrayCount
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

   if indexPath.row % 2 == 0 { 
    let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell

    cell.friendfirstnameLabel.text = "Michael"
    cell.friendImageView.image = UIImage(named: "mikepic")

    return cell
  } else {
    let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell

    cell.friendfirstnameLabel.text = "Michael"
    cell.friendImageView.image = UIImage(named: "mikepic")

    return cell
  }
}

您只需要使用分配的重用标识符对第二个单元格进行消隐。当您想使用第一个和第二个时,这完全取决于您的要求。

您只需根据
[indexPath row]
更改发送到
dequeueReusableCellWithIdentifier
的参数值即可。如果是0,则使用“firstCell”,如果是1,则使用“secondCell”并为匹配的对象类型创建一个单元格。

这里我看到您在单元格视图中显示了静态文本。通常,我们有一系列的数据来填充tableview中的数据,并在numberOfRowsInsuration方法中定义计数 所以,如果您知道要在哪个单元格中显示哪些数据,就很容易使用if语句来选择所需的表格cellview

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrayCount
}
检查计数后,您可以通过if语句执行此操作

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
NSUInteger row = [indexPath row]; 

if [yourArray objectAtIndex:row] =="Michael" { 
let cell = tableView.dequeueReusableCellWithIdentifier("FirstCell") as! FirstTableViewCell

cell.friendfirstnameLabel.text = "Michael"
cell.friendImageView.image = UIImage(named: "mikepic")

return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("SecondCell") as! SecondTableViewCell

cell.friendfirstnameLabel.text = “Unknown"
cell.friendImageView.image = UIImage(named: "mikepic")

 return cell
 }
}

我想在堆叠在一起的tableViewController中同时使用这两个选项。:-)您尝试过@Rajatp解决方案吗?这跟我说的差不多。其思想是使用deque方法向操作系统指示要对当前行使用哪种类型的单元格。如果您指示“firstCell”,它将为您提供该类型的单元格(如果有,否则,您将创建一个新单元格),如果您指示“secondCell”,它将为您提供一个匹配的单元格。这能让事情更清楚一点吗?我想我已经试过了你的建议,但我无法让它发挥作用。我尝试了:let cell=tableView.dequeueReusableCellWithIdentifier(“FirstCell”)as!FirstTableViewCell.friendfirstnameLabel.text=“Michael”cell.friendImageView.image=UIImage(名为:“mikepic”)让cell=tableView.dequeueReusableCellWithIdentifier(“SecondCell”)为!SecondTableViewCell.businessImageView.image=UIImage(名为:“bizpic”)注释中没有缩进,但您似乎缺少一个if语句来决定是否对此行使用“firstCell”或“secondCell”。您不能同时使用这两种方法,每次调用此方法时,您只处理一个单元格。我尝试了@rajapt solution,并为“yourArrayCount”输入了2,但在“if indexath%2==0{”行中,我得到一个错误,“使用未解析标识符'indexath'”应该是“indexath”(大写P),当我尝试此操作时,为“yourArrayCount”输入2,但在“if indexpath%2==0{”行中,我得到一个错误“使用未解析标识符'indexpath'”,只需将'indexpath'改为'indexpath'即可!感谢Rajatp和@Chonch。但是,现在当我运行它时,我在表中只看到“FirstCell”。“SecondCell”你只看到一个单元格还是第一个单元格的两倍?我只看到一个单元格——“第一个单元格”显示一次。