Ios 在tableview中使用两个不同的单元格
我想在tableview中显示两个不同的映射,我以前使用过这段代码,但现在Xcode抱怨该单元格没有成员Ios 在tableview中使用两个不同的单元格,ios,xcode,swift,mapkit,Ios,Xcode,Swift,Mapkit,我想在tableview中显示两个不同的映射,我以前使用过这段代码,但现在Xcode抱怨该单元格没有成员namelab,addressLabel。我错过什么了吗 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { var cell: UITableViewCell! if indexPath == 0 {
namelab
,addressLabel
。我错过什么了吗
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell: UITableViewCell!
if indexPath == 0 {
cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell
cell.nameLabel.text = ""
cell.addressLabel.text = ""
cell.cityLabel.text = ""
let inspectionDate: String = detailItem!["dato"] as! String
cell.inspectionDateLabel.text = self.convertDateString(inspectionDate)
}
else
{
cell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell
// Set map options
}
return cell
}
您的问题是,您正在将
单元格
作为UITableViewCell
键入,然后尝试将其用作您已退出队列的特定类型。您应该这样做:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell
if indexPath == 0 {
let summaryCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell
summaryCell.nameLabel.text = ""
summaryCell.addressLabel.text = ""
summaryCell.cityLabel.text = ""
let inspectionDate: String = detailItem!["dato"] as! String
summaryCell.inspectionDateLabel.text = self.convertDateString(inspectionDate)
cell = summaryCell
}
else
{
let mapCell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell
// Set map options
cell = mapCell
}
return cell
}
在这里,我将单元格作为特定类型进行排队,然后将
单元格设置为在返回之前指向它。执行以下操作:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath == 0 {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SummaryHeaderTableViewCell
cell.nameLabel.text = ""
cell.addressLabel.text = ""
cell.cityLabel.text = ""
let inspectionDate: String = detailItem!["dato"] as! String
cell.inspectionDateLabel.text = self.convertDateString(inspectionDate)
return cell
}
else
{
let cell = tableView.dequeueReusableCellWithIdentifier("MapCell", forIndexPath: indexPath) as! MapTableViewCell
// Set map options
return cell
}
}
我接受了这个答案,因为在没有两次回报的情况下,它更干净了一点。