ios - 如何在 swift 中的索引路径委托(delegate)的行的单元格中加载自定义表格 View 单元格

标签 ios swift uitableview

import UIKit

class NewOrdersViewControllers: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var items = []
    @IBOutlet var tableView:UITableView!

    @IBOutlet weak var lblRestaurantNames: UILabel!
    @IBOutlet weak var mapView: UIButton!

    var cellIdentifier = "cell"


    override func viewDidLoad() {
        super.viewDidLoad()

        //self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        let nib = UINib(nibName: "vwTblCell", bundle: nil)

        tableView.registerNib(nib, forCellReuseIdentifier: "cell")
        tableView.delegate = self

        tableView.dataSource = self

        // Do any additional setup after loading the view.
    }

    @IBAction func mapPush(sender: AnyObject) {

        let mapVC = MapViewController()

        self.navigationController?.pushViewController(mapVC, animated: true)
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

//            let cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier(cellIdentifier)! as! UITableViewCell
//
//            cell.textLabel?.text = self.items[indexPath.row] as! String
//
//            cell.RestaurantLbl.text = self.items[indexPath.row]
//
//            cell.lblrest.text = items[indexPath.row] as! String

        var cell:TblCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as! TblCell

        cell.lblRestaurantName.text = items[indexPath.row] as! String

        return cell
    }

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

        print("You selected cell #\(indexPath.row)!")
    }

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

        return 100
    }

最佳答案

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

        let identifier = "Custom"

        var cell: CustomCell! = tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCel  

          if cell == nil {
                tableView.registerNib(UINib(nibName: "CustomCell", bundle: nil), forCellReuseIdentifier: identifier)
               cell =tableView.dequeueReusableCellWithIdentifier(identifier) as? CustomCell
            }
          return cell
        }

关于ios - 如何在 swift 中的索引路径委托(delegate)的行的单元格中加载自定义表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34504643/

相关文章:

objective-c - 使用 NSDate 获取每年 7 月的日期

swift - 在 UIVIew 的左上角创建 UILabel 并直接向下设置动画?

swift - Firebase 身份验证被调用两次

iphone - 自定义 uitableviewcell 内的按钮不起作用

iOS 11 安全区域布局指南向后兼容

ios - UIAccessibilityElement `accessibilityCustomActions` 无法正常工作

ios - 如何在 Sprite Kit 中初始化所有场景?

ios - 将 JSON 数组解包为字符串值时遇到问题

ios - UITableView 更新正确数量的 managedObjects,但不会显示值

ios - 快速删除表格 View 中的一行