如何从Swift中的表格单元格视图中删除

时间:2015-04-01 01:54:45

标签: ios uitableview swift

这是我在主表视图控制器上所需的代码我只是不知道如何转换到新的视图控制器到选定的单元格来显示有关所选单元格的更多内容。

import UIKit

class MainVC: UITableViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet var Table1: UITableView!
var postArray: [[String]] = []

var postSubject = ""
var postDescription = ""
var postDate = ""

override func viewDidLoad() {
    super.viewDidLoad()
    fetchData()
}

func fetchData() {
    println("success1")
    postArray = []

    let httpMethod = "GET"

    let timeout = 15

    let urlAsString = "http://cgi.soic.indiana.edu/~team12/main_requests.php"
    let url = NSURL(string: urlAsString)!

    let urlRequest = NSMutableURLRequest(URL: url,
        cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData,
        timeoutInterval: 15.0)

    urlRequest.HTTPMethod = httpMethod

    let queue = NSOperationQueue()
    println("success2")

    NSURLConnection.sendAsynchronousRequest(urlRequest,
        queue: queue,
        completionHandler: {(response: NSURLResponse!,
            data: NSData!,
            error: NSError!) in

            if data.length > 0 && error == nil{
                let html = NSString(data: data, encoding: NSUTF8StringEncoding)

            } else if data.length == 0 && error == nil{
                println("Nothing was downloaded")
            } else if error != nil{
                println("Error happened = \(error)")
            }

            if data.length > 0 && error == nil {
                let html = NSString(data: data, encoding: NSUTF8StringEncoding)

                var newArrayofDicts : NSMutableArray = NSMutableArray()
                var arrayOfDicts : NSMutableArray? = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error:nil) as? NSMutableArray
                println("successmutable")

                if arrayOfDicts != nil {
                    for item in arrayOfDicts! {

                        if var dict  = item as? NSMutableDictionary{

                            if  dict["subject"] != nil{
                                self.postSubject = dict["subject"] as String
                                self.postDescription = dict["subject"] as String
                                self.postDate = dict["date"] as String
                            self.postArray.append([self.postSubject,self.postDescription,self.postDate])
                                println("successpost")

                            }
                        }
                    }
                }
            }
        }
    )
    sleep(1)
}


override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    fetchData()
    tableView.reloadData()
    println("successanime")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

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

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    println("successoverride")

    let cell = tableView.dequeueReusableCellWithIdentifier("UpdateCell", forIndexPath: indexPath)
        as UITableViewCell
    println("successupdatecell")

    cell.textLabel.text = postArray[indexPath.row][1]

    cell.detailTextLabel?.text = postArray[indexPath.row][2]

    println("success3")
    return cell
}

1 个答案:

答案 0 :(得分:0)

试试这个方法

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
...
}

或者您可以通过ctrl将原型单元格拖动到下一个viewController直接在故事板中完成。

希望这会有所帮助!!

修改

如果您想以编程方式转换到下一个view controller,那么您可以执行以下任一操作:

performSegueWithIdentifier("yourSegueName", sender: nil)

或者:

presentViewController(nextViewController, animated: true, completion: nil)