Parse Data into NSTableView

前端 未结 2 423
旧巷少年郎
旧巷少年郎 2021-02-04 19:01

Hello fellow programmers,

First off, I am new to OSX/iOS programming and am trying to learn swift. I am fairly competent in java and wanted to teach myself something new

2条回答
  •  一向
    一向 (楼主)
    2021-02-04 20:00

    The way we do it in iOS is, In the class that is the TableView's Delegate and Datasource we implement

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
    
    
        var ident = "cellIdentifier"
        var cell = self.tableView.dequeueReusableCellWithIdentifier(ident) as UITableViewCell
        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: ident)
        }
        cell.textLabel.text = myTitleArray[indexPath.row]
        return cell
    
    }
    

    To handle the user selecting a row:

    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
    

    }

    And to tell it how many rows there are:

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

    Where myTitleArray is your array of titles, obviously you can change that as you wish.

    The way you find out what you should actually put as var ident = "cellIdentifier" is the is the identifier you gave the dynamic prototype cell in the storyboard

    I agree sometimes the Apple Developer Class Reference can be confusing and often they have a Programming Guide which is way more helpful.

    Try looking here for what I understand is what you want.

    If your very interested in iOS programming check out these free Stanford lectures on iTunes U particularly 11, 12, and 13 talk about UITableView. They are in iOS 7 so nothing about Swift, but the only real difference in the implementation of UITableView is syntax between those languages

提交回复
热议问题