最新消息:服务器现已从linode换到fzhost.net,且主题已从retina 0.2换为大前端D8

【已解决】swift给UITableView设置外部的数据源

Swift crifan 12浏览 0评论

搜:

swift tableview datasource outside

参考:

ios – How to set datasource and delegate Outside of View Controller – Stack Overflow
ios – How can I programmatically set dataSource of UITableView? – Stack Overflow
看起来是:
针对于UITableViewDataSource(和UITableViewDelegate)
去创建一个类
然后UITableView中,new一个实例,然后设置到delegate和datasource上
TeamTableViewData.swift
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)")
    }
}
别的文件中初始化一个类:
var gTeamTableViewData = TeamTableViewData()
SwitchTeamTableView.swift
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设置外部的数据源

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
51 queries in 0.523 seconds, using 12.41MB memory