swift怎么使用tableview

 我来答
可以叫我表哥
推荐于2016-03-04 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
采纳数:25897 获赞数:1464999
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。

向TA提问 私信TA
展开全部

Xcode6新建一个项目,采用swift创建代码:

创建一个ViewController继承UITableViewController

涉及了模型,控制器

模型:ZLPlace.swift

class ZLPlace: NSObject {  
var place = ""  
var visited = false  

tableViewController 控制器

import UIKit  
 
class ViewController: UITableViewController {  
      
    // 静态数据数组,存放模型  
    var arrs = [ZLPlace]()  
      
    override func viewDidLoad() {  
        super.viewDidLoad()  
          
        let place2 = ZLPlace()  
        place2.place = "zhang2"  
        arrs.append(place2)  
          
        let place3 = ZLPlace()  
        place3.place = "zhang3"  
        arrs.append(place3)  
          
        let place4 = ZLPlace()  
        place4.place = "zhang1"  
        arrs.append(place4)  
          
        self.tableView.reloadData()  
    }  
      
    // 数据源方法, 返回多少组  
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {  
        return 1;  
    }  
      
    // 每组有多少行  
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {  
        return arrs.count;  
    }  
      
    // 每行展示什么内容  
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {  
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell  
          
        let place = arrs[indexPath.row]  
          
        cell.textLabel.text = place.place  
          
        return cell;  
          
    }  
      
    // 点击每个cell触发什么事件  
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {  
          
        let place = arrs[indexPath.row]  
        place.visited = !place.visited;  
          
        let cell = tableView.cellForRowAtIndexPath(indexPath)  
        cell?.backgroundColor = UIColor.clearColor()  
        if(place.visited){  
            cell?.accessoryType = UITableViewCellAccessoryType.Checkmark  
        }else{  
            cell?.accessoryType = UITableViewCellAccessoryType.None  
        }  
    }  
      
    // 点击编辑按钮  
    @IBAction func editing(sender: AnyObject) {  
        self.tableView.setEditing(true, animated: true)  
    }  
      
    // 删除每个cell  
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {  
        if editingStyle == UITableViewCellEditingStyle.Delete{  
            arrs.removeAtIndex(indexPath.row)  
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Top)  
        }  
    }  
      

效果如图:

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2014-12-07
展开全部
//
// JieTableViewController.swift
// JieTableView
//
// Created by jiezhang on 14-10-5.
// Copyright (c) 2014年 jiezhang. All rights reserved.
//

import UIKit

class JieTableViewController: UITableViewController {

var listVideos : NSMutableArray!

override func viewDidLoad() {
super.viewDidLoad()
var bundle = NSBundle.mainBundle()
let plistPath : String! = bundle.pathForResource("videos", ofType: "plist")
listVideos = NSMutableArray(contentsOfFile: plistPath)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式