搜:
swift tableview datasource outside
参考:
看起来是:
针对于UITableViewDataSource(和UITableViewDelegate)
去创建一个类
然后UITableView中,new一个实例,然后设置到delegate和datasource上
TeamTableViewData.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | import UIKit struct TeamItem { var teamID:String = "" var nameStr:String = "" } class TeamTableViewData: UIViewController, UITableViewDataSource, UITableViewDelegate{ var teamItemArr:[TeamItem] = [TeamItem]() func updateTeamData(){ var teamItem:TeamItem teamItem = TeamItem() teamItem.nameStr = “team1" teamItem.teamID = "TeamID_team1" teamItemArr.append(teamItem) teamItem = TeamItem() teamItem.nameStr = “team2" teamItem.teamID = "TeamID_team2" teamItemArr.append(teamItem) } /*************************************************************************** * UITableViewDataSource functions ***************************************************************************/ func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { //print("numberOfRowsInSection indexPath = \(indexPath)") return teamItemArr.count } func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { //print("heightForRowAtIndexPath indexPath = \(indexPath)") return HeightSwitchTeamCell } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { print( "cellForRowAtIndexPath indexPath = \(indexPath)" ) let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "StrIdSwitchTeamTableViewCell" ) cell.backgroundColor = UIColor.whiteColor() cell.selectionStyle = UITableViewCellSelectionStyle.Blue cell.accessoryType = UITableViewCellAccessoryType.None cell.accessoryView = nil; cell.detailTextLabel?.text = nil cell.textLabel?.font = UIFont.boldSystemFontOfSize(14) cell.textLabel?.textAlignment = NSTextAlignment.Left cell.textLabel?.textColor = UIColor.grayColor() cell.textLabel?.text = teamItemArr[indexPath.row].nameStr cell.imageView?.image = UIImage(named: "add_conversation" ) return cell } /*************************************************************************** * UITableViewDelegate functions ***************************************************************************/ func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { print( "didSelectRowAtIndexPath indexPath = \(indexPath)" ) } } |
别的文件中初始化一个类:
1 | var gTeamTableViewData = TeamTableViewData() |
SwitchTeamTableView.swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import UIKit class SwitchTeamTableView: UIView{ var switchTeamTableView:UITableView = UITableView() var switchTeamFrame:CGRect = CGRectZero override init(frame:CGRect) { switchTeamFrame = frame switchTeamTableView = UITableView(frame: CGRectMake( 0, 0, switchTeamFrame.width, switchTeamFrame.height) ) print( "switchTeamTableView.frame=\(switchTeamTableView.frame)" ) super.init(frame:switchTeamFrame) switchTeamTableView.delegate = gTeamTableViewData switchTeamTableView.dataSource = gTeamTableViewData switchTeamTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "StrIdSwitchTeamTableViewCell" ) self.addSubview(switchTeamTableView) } required init?(coder aDecoder: NSCoder) { fatalError( "init(coder:) has not been implemented" ) } |
即可。
转载请注明:在路上 » 【已解决】swift给UITableView设置外部的数据源