gpt4 book ai didi

swift - 在循环中创建多个 TableView (快速)

转载 作者:行者123 更新时间:2023-11-28 08:30:32 26 4
gpt4 key购买 nike

是否可以在 for 循环中创建多个 tableview?我曾尝试这样做但失败了。有可能这样做吗?我必须在我的 View Controller 中创建几个 TableView 。

for var i in 0...1{
var tableView = UITableView()
tableView = UITableView(frame: CGRectMake(0,0,500,100))

tableView.separatorColor = UIColor.clearColor()

tableView.dataSource = self
tableView.delegate = self

self.view.addSubview(tableView)

showsArray = ["House of Cards","Arrested Development","Orange is the New Black","Unbreakable","Daredevil","The Killing","BoJack Horseman","Mad Men","Breaking Bad","Bates Motel"]
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return showsArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cellFrame = CGRectMake(0, 0, self.tableView.frame.width, 52.0)
var retCell = UITableViewCell(frame: cellFrame)

var textLabel = UILabel(frame: CGRectMake(10.0, 0.0, UIScreen.mainScreen().bounds.width - 20.0, 52.0 - 4.0))
textLabel.textColor = UIColor.blackColor()
textLabel.text = showsArray[indexPath.row]
retCell.addSubview(textLabel)

return retCell
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return 52.0
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}

func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
if section == 0
{ return 64.0
}

return 32.0
}

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let ret = UILabel(frame: CGRectMake(10, 0, self.tableView.frame.width - 20, 32.0))
ret.backgroundColor = UIColor.clearColor()
ret.text = "TV Shows"
ret.textAlignment = NSTextAlignment.Center
return ret
}

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]?
{ let more = UITableViewRowAction(style: .Normal, title: "More") { action, index in
print("more button tapped")
}
more.backgroundColor = UIColor(patternImage: UIImage(named: "Unknown.jpg")!)

let favorite = UITableViewRowAction(style: .Normal, title: "Favorite") { action, index in
print("favorite button tapped")
}
favorite.backgroundColor = UIColor.orangeColor()

let share = UITableViewRowAction(style: .Normal, title: "Share") { action, index in
print("share button tapped")
}
share.backgroundColor = UIColor.blueColor()

return [share, favorite, more]
}

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
// the cells you would like the actions to appear needs to be editable
return true
}

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
// you need to implement this method too or you can't swipe to display the actions
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

定义与之前的值冲突。

最佳答案

这是可能的。您需要保持对数据源的强烈引用。最好把它放在单独的类(class)里。看看这里creating UITableViews in array block enumeration causes crash

关于swift - 在循环中创建多个 TableView (快速),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38948686/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com