2016-08-15 11 views
1

forループ内で複数のテーブルビューを作成することは可能ですか?私はそれをしようとしましたが失敗します。それは可能ですか?私は私のビューコントローラでいくつかのテーブルビューを作成する必要があります。forループ内で複数のテーブルビューを作成する(swift)

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. 
} 


} 

定義が以前の値と競合しています。

+0

私はXYの問題を臭い... – Sweeper

+0

私はあなたのケースでは、あなたはテーブルよりも複数のセクションを使うべきだと思います – xmhafiz

答えて

関連する問題