简体   繁体   English

iOS Swift - 自定义UITableViewCell无法使用标签栏

[英]iOS Swift - Custom UITableViewCell not working with tab bar

I'm trying to create a table view with custom cells using Swift. 我正在尝试使用Swift创建一个包含自定义单元格的表视图。 The same code works in a single page application, but in a tab based app it adds a gray layer over the table view content. 相同的代码适用于单个页面应用程序,但在基于选项卡的应用程序中,它会在表格视图内容上添加灰色图层。

This is my custom cell class: 这是我的自定义单元类:

class CardCell:UITableViewCell {
    @IBOutlet var backImg: UIImageView!
    @IBOutlet var testButton: UIButton!

    func loadItem(var title:String) {
        backImg.image = UIImage(named: title)
    }
}

Here is the viewController: 这是viewController:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var items = ["swift 1.jpeg", "swift 2.jpeg"]

    @IBOutlet var tableView:UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        var nib = UINib(nibName: "CardCell", bundle: nil)
        tableView.registerNib(nib, forCellReuseIdentifier: "CardCell")
    }

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

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        var cell:CardCell = tableView.dequeueReusableCellWithIdentifier("CardCell") as CardCell
        var (title) = items[indexPath.row]
        cell.loadItem(title)
        return cell
    }
}

You have to use UiTableViewController Class It's Better. 你必须使用UiTableViewController Class It's Better。 If you have to use UiView Controller Then : 如果你必须使用UiView控制器然后:

import UIKit
import QuartzCore

class OthersViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, MFMailComposeViewControllerDelegate {

    /*init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        // Custom initialization
    }*/

    let arrData = ["Row 1","Row 2","Row 3","Row 4","Row 5"];
    let dict = NSMutableDictionary()
    let arrImage = ["image 1","image 2","image 3","image 4","image 5","image 6"];
    let kLCellIdentifier = "CellIdentifier"
    var tblVc = UITableView();

    override func viewDidLoad() {
        dict.setObject(arrData, forKey: "dsd")

        super.viewDidLoad()
        self.title = "Others"
        self.view.backgroundColor = UIColor.clearColor()
        tblVc.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
        tblVc.delegate = self;
        tblVc.dataSource = self;
        tblVc.tableFooterView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 0))
        tblVc.backgroundColor = UIColor.clearColor();
        tblVc.separatorColor = UIColor.redColor();
        self.view.addSubview(tblVc);

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

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

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
        var cell = tableView.dequeueReusableCellWithIdentifier(kLCellIdentifier) as UITableViewCell!
        if !cell {
            cell = UITableViewCell(style:.Default, reuseIdentifier: kLCellIdentifier)
        }

        cell.backgroundColor = UIColor.clearColor()
        cell.textLabel.text = arrData[indexPath.row]
        cell.image = UIImage(named: "\(arrImage[indexPath.row])");

        cell.accessoryType  = UITableViewCellAccessoryType.DisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyle.None;

        return cell;
    }
    func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat
    {
        return 60;
    }
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        switch (indexPath.row) {
        case 0:
        break;
        case 1:

            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
        case 5:
            break;
        default:
            break;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM