SWIFT:表格视图中的 2 个自定义单元格。怎么了?



我想在TableView中放入2个自定义单元格,但我有一个小问题。我试图在Google和StackOverflow中找到答案,但答案对我没有帮助。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0{
        var cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("FistCustomTableViewCell", forIndexPath: indexPath) as CustomCell
        //set cell2
    }
    if indexPath.row >= 1{
        var cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell", forIndexPath: indexPath) as CustomCell
        let cons = aArray[indexPath.row - 1]
        // set cell2 
    }
    return cell // (!) Use of unresolved identifier "cell"
}

您需要在外部绘制单元格声明:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
  var cell: CustomCell!
  if indexPath.row == 0{
    cell = tableView.dequeueReusableCellWithIdentifier("FistCustomTableViewCell", forIndexPath: indexPath) as CustomCell
    //set cell2
  }
  if indexPath.row >= 1{
    cell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell", forIndexPath: indexPath) as CustomCell
    let cons = aArray[indexPath.row - 1]
    // set cell2 
  }
  return cell
}

您必须从if语句中声明单元格。

最新更新