最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Swift crifan 2546浏览 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,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.176 seconds, using 21.99MB memory