UITableView Using Swift

前端 未结 10 1441
温柔的废话
温柔的废话 2020-12-08 07:27

My TapCell1.swift

This is Custom UITableViewCell View

import UIKit

class TapCell1: UITableViewCell
{
    @IBOutlet var         


        
相关标签:
10条回答
  • 2020-12-08 08:06

    Check your Story board select the cell and look at the "identity inspector in that select CLASS type your CustomClass and MODULE type your project name

    I have done this It works perfectly try this tip to avoid error to see below image and code

    override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell? {
    
     // let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: nil)
    
      //  cell.textLabel.text = array[indexPath!.row] as String
    
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTableViewCell
    
        cell.nameLabel.text = array[indexPath!.row] as String
        cell.RestaurentView.image = UIImage(named:images[indexPath!.row])
    
        return cell
    }
    
    0 讨论(0)
  • 2020-12-08 08:09

    I have now been able to get Custom UITableViewCell to work.

    Works on

    • Runs on Xcode 6 beta 6
    • Runs on Xcode 6 beta 5

    • iOS is 7.1

    How

    • I have created a ".xib" file for the cell.
    • I copy it into the storyboard.
    • Via the storyboard I give it a Identifier.
    • I make sure it is a sub child of a tableview

    Doing it this way, you do not need to register a class / nib etc.

    This is my custom cell.

    import UIKit
    
    class TestCell: UITableViewCell {
    
        @IBOutlet var titleImageView: UIImageView!
        @IBOutlet var titleLabel: UILabel!
    
        override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
            super.init(style: style, reuseIdentifier: reuseIdentifier)
        }
    
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    }
    

    In your view, or where ever you extend "UITableViewDataSource". Make sure "cell2" is the same as the "Identifier" that you gave it via the storyboard.

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    
        var cell:TestCell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as TestCell
    
        // Example of using the custom elements.
        cell.titleLabel.text = self.items[indexPath.row]
    
        var topImage = UIImage(named: "fv.png")
        cell.titleImageView.image = topImage
    
        return cell
    }
    

    uitableviewcell

    0 讨论(0)
  • 2020-12-08 08:11

    Extention of NSOject

    extension NSObject {    
        var name: String {
            return String(describing: type(of: self))
        } 
        class var name: String {
            return String(describing: self)
        }
    }
    

    Properties on Custom TableViewCell

    class var nib: UINib {
        return UINib(nibName:YourTableViewCell.name, bundle: nil)
    }
    class var idetifier: String {
        return YourTableViewCell.name
    }
    

    Add in UIViewController

    self.tableView.registerNib(YourTableViewCell.nib, forCellReuseIdentifier: YourTableViewCell.idetifier)
    
    let cell = tableView.dequeueReusableCellWithIdentifier(YourTableViewCell.idetifier, forIndexPath: indexPath) as! YourTableViewCell
    
    0 讨论(0)
  • 2020-12-08 08:12

    You should register the class for the cell. For that do change this line of code to

    self.tableView.registerClass(TapCell1.classForCoder(), forCellReuseIdentifier: "Cell")
    

    Edit

    You code is looks fine i checked it

    //cell.labelText.text="Cell Text"
    
    cell.textLabel.text="Cell Text"   // use like this
    
    0 讨论(0)
  • 2020-12-08 08:19

    Try this following code

    var cell:CustomTableViewCell = tableView.dequeueReusableCellWithIdentifier("CustomTableViewCell") as CustomTableViewCell

    https://github.com/iappvk/TableView-Swift

    0 讨论(0)
  • 2020-12-08 08:22

    If you are not using Storyboard then create a new file as subclass of UITableViewCell check the checkbox "also create xib files" after that set your custom cell .and here is the code for tableview

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
       {
    
        var customcell:CustomTableViewCellClass? = tableView.dequeueReusableCellWithIdentifier("cell") as? CustomTableViewCellClass
        if  (customcell==nil)
        {
            var nib:NSArray=NSBundle.mainBundle().loadNibNamed("CustomTableViewCellClass", owner: self, options: nil)
    
            customcell = nib.objectAtIndex(0) as? CustomTableViewCell
        }
    
        return customcell!
    
    
    }
    
    0 讨论(0)
提交回复
热议问题