在for循环中创建多个tableview(swift)

时间:2016-08-15 02:51:27

标签: swift

是否可以在for循环中创建多个tableview?我试图这样做但失败了。有可能吗?我必须在我的视图控制器中创建几个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.
}


}

定义与先前值冲突。

1 个答案:

答案 0 :(得分:0)

有可能。你需要保持对dataSource的强引用。将它放在单独的课程中会更好。看看creating UITableViews in array block enumeration causes crash