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

【已解决】Swift创建列表视图TableView

Swift crifan 2432浏览 0评论

【背景】

折腾:

【已解决】Swift创建新的页面视图并添加一个列表视图

期间,继续去在新添加的页面中,添加列表table的视图:tableview。

 

【折腾过程】

1.参考已有的别人代码。

2.搜:

swift create table view

参考:

How to make a simple tableview with iOS 8 and Swift

Start Developing iOS Apps (Swift): Create a Table View

Swift 2 Tutorial Part 3: Tuples, Protocols, Delegates, and Table Views – Ray Wenderlich

【总结】

后来是参考别人的代码后,经过整理,可以生成table和cell了:

1.首先是当前类,要继承UITableViewDelegate, UITableViewDataSource

2.再去实现对应的四个函数:

  • numberOfRowsInSection:有几行
  • heightForRowAtIndexPath:分别返回每行的高度
  • cellForRowAtIndexPath:分别返回每一行的cell,包含了具体的内容的设置,比如label,image,右边是打对勾还是右箭头,等等
  • didSelectRowAtIndexPath:当选中了某行,如何处理。比如是跳转到新的子页面。

相关代码如下:

import UIKit

class MyWalletViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var itemNameList = ["item1", "item2", "item3", "item4"]

    var tableView:UITableView?

    init() {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let backImage = UIImage(named: "back.png")
        let backBtn = UIButton()
        backBtn.frame = CGRectMake(10, 30, 24, 24)
        backBtn.addTarget(self, action:Selector("backToPrevWindow:"), forControlEvents:UIControlEvents.TouchUpInside)
        backBtn.setImage(backImage, forState:UIControlState.Normal)
        backBtn.setImage(backImage, forState: UIControlState.Selected)
        self.view.addSubview(backBtn)

        let titleLabel = UILabel()
        titleLabel.frame = CGRectMake(40, 0, self.view.bounds.width - 80, 44)
        titleLabel.text = "我的钱包"
        titleLabel.textColor = UIColor.whiteColor()
        titleLabel.font = UIFont.boldSystemFontOfSize(18)
        titleLabel.textAlignment = NSTextAlignment.Center
        tabbgView.addSubview(titleLabel)


        //创建表格视图
        print(self.itemNameList)
        self.tableView = UITableView(frame: CGRectMake(0, 70, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)))
        self.tableView!.delegate = self
        self.tableView!.dataSource = self
        self.tableView?.backgroundColor = UIColor(hexString: "#e6e6e6")
        self.tableView?.separatorColor = UIColor(hexString: "#dbdbdb")
        self.tableView?.separatorInset = UIEdgeInsetsMake(0, 10, 0, 0)

        //创建一个重用的单元格
        self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwiftCell")
        self.view.addSubview(tableView!)
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.itemNameList.count
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 55
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        print("cellForRowAtIndexPath indexPath = \(indexPath)")
        let indentify:String = "SwiftCell"

        let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: indentify)

        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.backgroundColor = UIColor.whiteColor()

        cell.textLabel?.text = self.itemNameList[indexPath.row]
        cell.textLabel?.textColor = UIColor(hexString: "#4d4d4d")
        cell.textLabel?.font = UIFont.boldSystemFontOfSize(15)

        cell.detailTextLabel?.text = "0.0元"
        cell.detailTextLabel?.textColor = UIColor.redColor();
        cell.detailTextLabel?.font = UIFont.boldSystemFontOfSize(13)
        cell.detailTextLabel?.textAlignment = NSTextAlignment.Right

        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.tableView!.deselectRowAtIndexPath(indexPath, animated: true)
        self.tableView!.cellForRowAtIndexPath(indexPath)!.selected = false

        switch indexPath.row {
        case 0:

            break
        case 1:

            break;
        case 2:

            break;
        case 3:

            break;

        case 4:

            break;

        default:

            break;
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    func backToPrevWindow(button : UIButton) {
        self.navigationController?.popViewControllerAnimated(true)
    }

}

供参考。

转载请注明:在路上 » 【已解决】Swift创建列表视图TableView

发表我的评论
取消评论

表情

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

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