iOS Swift - 自定义 UITableViewCell 不适用于标签栏

标签 ios uitableview swift uitabbar

我正在尝试使用 Swift 创建带有自定义单元格的 TableView 。相同的代码适用于单页应用程序,但在基于选项卡的应用程序中,它会在表格 View 内容上添加一个灰色层。

这是我的自定义单元格类:

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

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

这是 View Controller :

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
    }
}

最佳答案

你必须使用 UiTableViewController 类才好。 如果你必须使用 UiView Controller 那么:

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;
        }
    }

关于iOS Swift - 自定义 UITableViewCell 不适用于标签栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24569303/

相关文章:

ios - 如何在 if/switch 语句中声明一个将变为全局的变量?

ios - 如何启用用户注册 - eJabberd for mac?

android - 使用 Flutter 从方法中获取一些变量值

ios - 将 iOS 应用程序 console.log 重定向到终端

objective-c - 单元格中的 UITextView CornerRadius

ios - 在 UITableViewCells 上使用动态高度时的 UITableView ContentSize

ios - 如何使用 AppDelegate 在 View 之间共享 iAd 横幅

ios - 单击另一个 UITextView 时结束编辑

在首次应用程序启动时将数据快速下载到本地存储

ios - 通过 Messenger 共享对话框共享链接后如何摆脱返回 Messenger