[英]Multiple implementation of custom uitableviewcell using dynamic prototype cells
Three different, dynamic prototype tableviewcells are used in my project. 在我的项目中使用了三种不同的动态原型tableviewcell。
Two are stock cells(basic with different options), and one is a custom cell. 两个是备用单元(基本具有不同的选项),一个是自定义单元。 And this, may change as required. 而且,这可能会根据需要进行更改。
The problem I face is while dequeuing the cells. 我面临的问题是出队时。
If they are done normally as UITableViewCells, I'm not able to use my custom cell outlets or actions. 如果它们通常作为UITableViewCells完成,则无法使用自定义单元格出口或操作。
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
And, If they are done the other way, the app crashes with the below error. 而且,如果以其他方式完成操作,则该应用程序将崩溃并显示以下错误。
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
Error: 错误:
Could not cast value of type 'UITableViewCell' (0x38595c58) to 'AppName.SwitchTableViewCell' (0x13cdf8).
Below is the implementation of cellForRowAtIndexPath
method, 下面是cellForRowAtIndexPath
方法的实现,
// MARK: - UITableViewDelegate Methods
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Setup a cellIdentifer string to store the cell reuse identifier you want to use for each row.
var cellIdentifier: String
// Switch through each row and set the appropriate cell reuse identifier
switch sections[indexPath.section].items[indexPath.row] {
case .AudioDevices, .Acknowledgments:
cellIdentifier = "DisclosureCell"
case .AllowNotifications, .ShowCloudMusic:
cellIdentifier = "SwitchCell"
case .Version:
cellIdentifier = "RightDetailCell"
}
// Populate your cell reuse identifier into the cell
if cellIdentifier == "SwitchCell" {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
return cell
}
// Switch through each cell, and implement the labels/setup for each row
// The order of the cases is irrelevant!
switch sections[indexPath.section].items[indexPath.row] {
case .AudioDevices:
cell.textLabel?.text = "Audio Devices"
cell.detailTextLabel?.text = String(userDefaults.integerForKey(audioDeviceListKey))
case .AllowNotifications:
cell.switchCellLabel?.text = "Allow Notifications"
cell.userInteractionEnabled = false
case .ShowCloudMusic:
cell.switchCellLabel?.text = "Show Cloud Music"
case .Acknowledgments:
cell.textLabel?.text = "Acknowledgements"
cell.detailTextLabel?.text = ""
case .Version:
cell.textLabel?.text = "Version"
cell.detailTextLabel?.text = buildVersion
cell.detailTextLabel?.textColor = UIColor.lightGrayColor()
cell.userInteractionEnabled = false
}
// Return the cell
return cell
}
Screenshot: 屏幕截图:
To use multiple kinds of cell in Table View, you have to put some condition to check to see which cell be used for certain condition. 要在“表视图”中使用多种单元格,必须放置一些条件以检查哪个单元格用于特定条件。
func tableView cellForRowAtInd....... {
//depends on what base you separate both cells
if condition = true {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath)
return cell
}
}
Update based upon your cellForRowAtIndexPath
code: 根据您的cellForRowAtIndexPath
代码进行更新:
You are already returning before cell before your other part of code run. 您已经在单元的另一部分运行之前返回到单元格之前。 Hence the cell is already returned and its of no use for other part of code. 因此,该单元格已经返回,并且对于其他代码部分没有用处。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
// Setup a cellIdentifer string to store the cell reuse identifier you want to use for each row.
var cellIdentifier: String
// Switch through each row and set the appropriate cell reuse identifier
switch sections[indexPath.section].items[indexPath.row] {
case .AudioDevices, .Acknowledgments:
cellIdentifier = "DisclosureCell"
case .AllowNotifications, .ShowCloudMusic:
cellIdentifier = "SwitchCell"
case .Version:
cellIdentifier = "RightDetailCell"
}
// Populate your cell reuse identifier into the cell
if cellIdentifier == "SwitchCell" {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
// Switch through each cell, and implement the labels/setup for each row
// The order of the cases is irrelevant!
switch sections[indexPath.section].items[indexPath.row] {
case .AudioDevices:
cell.textLabel?.text = "Audio Devices"
cell.detailTextLabel?.text = String(userDefaults.integerForKey(audioDeviceListKey))
case .AllowNotifications:
cell.switchCellLabel?.text = "Allow Notifications"
cell.userInteractionEnabled = false
case .ShowCloudMusic:
cell.switchCellLabel?.text = "Show Cloud Music"
case .Acknowledgments:
cell.textLabel?.text = "Acknowledgements"
cell.detailTextLabel?.text = ""
case .Version:
cell.textLabel?.text = "Version"
cell.detailTextLabel?.text = buildVersion
cell.detailTextLabel?.textColor = UIColor.lightGrayColor()
cell.userInteractionEnabled = false
}
// Return the cell
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
// Switch through each cell, and implement the labels/setup for each row
// The order of the cases is irrelevant!
switch sections[indexPath.section].items[indexPath.row] {
case .AudioDevices:
cell.textLabel?.text = "Audio Devices"
cell.detailTextLabel?.text = String(userDefaults.integerForKey(audioDeviceListKey))
case .AllowNotifications:
cell.switchCellLabel?.text = "Allow Notifications"
cell.userInteractionEnabled = false
case .ShowCloudMusic:
cell.switchCellLabel?.text = "Show Cloud Music"
case .Acknowledgments:
cell.textLabel?.text = "Acknowledgements"
cell.detailTextLabel?.text = ""
case .Version:
cell.textLabel?.text = "Version"
cell.detailTextLabel?.text = buildVersion
cell.detailTextLabel?.textColor = UIColor.lightGrayColor()
cell.userInteractionEnabled = false
}
// Return the cell
return cell
}
}
plz use this 请使用这个
if cellIdentifier == "SwitchCell" {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! SwitchTableViewCell
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
return cell
}
access the label with tag like this in that cell where yon not define outlets 在未定义出口的单元格中访问带有此类标签的标签
if let theLabel = self.view.viewWithTag(123) as? UILabel {
theLabel.text = "some text"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.