Tuesday, 10 March 2015

swift learning -- tableview and show detail



In my previous post about objective-c tableview , it explains 3 key functions of implementing tableview.

You can simply copy the code and put them into your swift file, with some changes.



To make your cell clickable and show the detail page, you should create "selectiong segue" from your cell to a new view controller.

if you don't know how to do that, you can follow:
http://ios-blog.co.uk/tutorials/developing-ios8-apps-using-swift-part-4-adding-interactions/
http://www.raywenderlich.com/81880/storyboards-tutorial-swift-part-2


But one thing I must notice you guys that:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

In this function, you can't create cell your self like this:

var cell:UITableViewCell? = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "ordercell")
cell?.textLabel?.text = orderlist[indexPath.row];
return cell!

that will create the cell, however, your cell is not clickable.

you should:



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let kCellIdentifier: String = "ordercell"
        var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as UITableViewCell?
        if (cell == nil) {
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: kCellIdentifier)
        }

        cell?.textLabel?.text = orderlist[indexPath.row];
        return cell!

    }


































No comments:

Post a comment