Programmatically UITableView using swift

前端 未结 4 1591
广开言路
广开言路 2020-12-30 00:59

I\'m trying to create a simple tableView programmatically using swift, so I wrote this code on \"AppDelegate.swift\" :

    func application(application: UIA         


        
相关标签:
4条回答
  • 2020-12-30 01:24

    It is as simple as writing a function

        func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
    {
        let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")
    
        cell.text = self.Myarray[indexPath.row]
        cell.textLabel.textColor = UIColor.greenColor()
    
        cell.detailTextLabel.text = "DummyData #\(indexPath.row)"
        cell.detailTextLabel.textColor = UIColor.redColor()
        cell.imageView.image = UIImage(named:"123.png")
        return cell
    }
    
    0 讨论(0)
  • 2020-12-30 01:34

    In Xcode 6 Beta 4

    Removing

    init(style: UITableViewStyle) {
        super.init(style: style)
    }
    

    will do the trick. This is caused by different initializer behavior between Obj-C and Swift. You have created a designated initializer. If you remove it, all initializers will be inherited.

    The root cause is probably in -[UITableViewController initWithStyle:] which calls

    [self initWithNibName:bundle:]
    

    I actually think this might be a bug in the way Obj-C classes are converted to Swift classes.

    0 讨论(0)
  • 2020-12-30 01:41

    Cell Function Use:

    override 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.DetailDisclosureButton
        cell.selectionStyle = UITableViewCellSelectionStyle.None
        return cell
    }
    
    0 讨论(0)
  • 2020-12-30 01:48

    Instead of

    init(style: UITableViewStyle) {
        super.init(style: style)
    }
    

    you might find this handy:

    convenience init() {
        self.init(style: .Plain)
        title = "Plain Table"
    }
    

    Then, you can just call TableViewController() to initialize.

    0 讨论(0)
提交回复
热议问题