我制作了一个包含折叠单元格的表视图,我跟着这个tutorial;一切都很好,除了segue方法。我只是尝试添加performsegueWithIdentifier
方法(通过单元格在故事板中添加segue)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
performSegueWithIdentifier("toChantController", sender: self)
}
和prepareForSegue
方法:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toChantController"
{
let indexPath = tableView.indexPathForCell(sender as! UITableViewCell)
let controller = segue.destinationViewController as! ChantViewController
controller.chant = "\(sections[indexPath!.row])"
}
}
但Xcode向我发送此错误
“无法将'iSupporters.TeamChantViewController'类型的值(0x104562730)转换为'UITableViewCell'(0x106600540)。”
全班同学:
import UIKit
class TeamChantViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
// MARK: properties
var teamChants: TeamModel!
@IBOutlet weak var tableView: UITableView!
struct Section {
var name: String!
var items: [String]!
var collapsed: Bool!
init(name: String, items: [String], collapsed: Bool = true) {
self.name = name
self.items = items
self.collapsed = collapsed
}
}
var sections = [Section]()
override func viewDidLoad()
{
super.viewDidLoad()
tableView.backgroundColor = UIColor.clearColor()
tableView.separatorColor = UIColor.clearColor()
sections = [
Section(name: "Juventus", items: ["Olè", "fino alla fine", "ovunque voi giocate", "juve olè"]),
Section(name: "Derby", items: ["toro merda", "odio i granata", "il viola è il colore che odio"]),
Section(name: "Giocatori", items: ["Vidal", "Pogba", "Del Piero"])
]
}
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = true
}
// MARK: collection view data source and delegate
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
var count = sections.count
for section in sections
{
count += section.items.count
}
return count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
let section = getSectionIndex(indexPath.row)
let row = getRowIndex(indexPath.row)
if row == 0 {
return 50.0
}
return sections[section].collapsed! ? 0 : 44.0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let section = getSectionIndex(indexPath.row)
let row = getRowIndex(indexPath.row)
if row == 0
{
let cell = tableView.dequeueReusableCellWithIdentifier("teamChantsHeader") as! TeamChantsHeader
cell.teamChantSectionTitle.text = sections[section].name
cell.toggleButton.tag = section
cell.toggleButton.setTitle(sections[section].collapsed! ? "+" : "-", forState: .Normal)
cell.toggleButton.addTarget(self, action: #selector(TeamChantViewController.toggleCollapse), forControlEvents: .TouchUpInside)
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier("teamChantsCell") as UITableViewCell!
cell.textLabel?.text = sections[section].items[row - 1]
return cell
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
performSegueWithIdentifier("toChantController", sender: self)
print(self)
}
// MARK: navigation (segue)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if segue.identifier == "toChantController"
{
let indexPath = tableView.indexPathForCell(sender as! UITableViewCell)
let controller = segue.destinationViewController as! ChantViewController
controller.chant = "\(sections[indexPath!.row])"
}
}
// MARK: - other methods
func toggleCollapse(sender: UIButton)
{
let section = sender.tag
let collapsed = sections[section].collapsed
// Toggle collapse
sections[section].collapsed = !collapsed
let indices = getHeaderIndices()
let start = indices[section]
let end = start + sections[section].items.count
tableView.beginUpdates()
for i in start ..< end + 1
{
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: i, inSection: 0)], withRowAnimation: .Automatic)
}
tableView.endUpdates()
}
func getSectionIndex(row: NSInteger) -> Int
{
let indices = getHeaderIndices()
for i in 0..<indices.count
{
if i == indices.count - 1 || row < indices[i + 1]
{
return i
}
}
return -1
}
func getRowIndex(row: NSInteger) -> Int
{
var index = row
let indices = getHeaderIndices()
for i in 0..<indices.count
{
if i == indices.count - 1 || row < indices[i + 1]
{
index -= indices[i]
break
}
}
return index
}
func getHeaderIndices() -> [Int]
{
var index = 0
var indices: [Int] = []
for section in sections
{
indices.append(index)
index += section.items.count + 1
}
return indices
}
}
有人可以帮助我执行这个赛段吗?
答案 0 :(得分:0)
你在这做两件事,
UITableViewCell
到ChantViewController
的stroyboard中创建了segue。didSelectRowAtIndexPath
您正在再次执行segue,并将self
作为TeamChantViewController
的参考。你需要改变任何一件事来解决你的问题。
编辑:根据您的评论,您需要在此sender
方法中使用performSegue
参数传递indexPath对象,并在prePareForSegue
方法中获取该indexPath。 / p>
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("toChantController", sender: indexPath)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toChantController"
{
let indexPath = sender as! NSIndexPath
let controller = segue.destinationViewController as! ChantViewController
controller.chant = "\(sections[indexPath!.row])"
}
}