按文本排序表格

时间:2016-06-22 14:00:32

标签: swift uitableview sorting tableview nsuserdefaults

我有一个名字,每个名字都转到另一个包含一个表(同一个)的视图控制器。我需要根据它的名称来区分tableView中显示的数据。

到目前为止,这是我的代码: 我点击了我点击名称的位置,它进入我的表视图,但每个表都具有相同的确切数据,并且不会根据名称进行更改。顺便说一下,finishAdding意味着在我的表中添加一段新数据(应该只显示在一个名称上)。

import UIKit

class EachInfo: UIViewController, UITableViewDelegate, UITableViewDataSource {


@IBOutlet weak var addLogBtn: UIButton!

@IBOutlet weak var logTable: UITableView!

@IBOutlet weak var logBoxNew: UITextView!

var toDoData:NSDictionary = NSDictionary()
var logItems:NSMutableArray = NSMutableArray()

var oppoName = ""


override func viewDidAppear(animated: Bool) {
    // loads all instead of certain ones
    let userDefaultsL:NSUserDefaults = NSUserDefaults.standardUserDefaults()

    let itemListFromUserDefaultsL:NSMutableArray? = userDefaultsL.objectForKey("itemList") as? NSMutableArray

    if (itemListFromUserDefaultsL != nil){
        logItems = itemListFromUserDefaultsL!
    }

    self.logTable.reloadData()
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    self.view.endEditing(true)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var logCell: UITableViewCell = self.logTable.dequeueReusableCellWithIdentifier("logcell")! as UITableViewCell
    var singleLogItem: NSDictionary = logItems.objectAtIndex(indexPath.row) as! NSDictionary
    logCell.textLabel?.text = singleLogItem.objectForKey("itemLog") as? String
    return logCell
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return logItems.count
}

override func viewDidLoad() {
    super.viewDidLoad()


   self.logTable.registerClass(UITableViewCell.self, forCellReuseIdentifier: "logcell")
    self.logTable.dataSource = self

    // Do any additional setup after loading the view.
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/



// change this to the opponentz platform and format. not actual software though
@IBAction func addLogAction(sender: AnyObject) {



}
@IBAction func finishAdding(sender: AnyObject) {
    var userDefaultsL:NSUserDefaults = NSUserDefaults.standardUserDefaults()

    var singleLogItem:NSMutableArray? = userDefaultsL.objectForKey("itemList") as? NSMutableArray



    var DataSet:NSMutableDictionary = NSMutableDictionary()
    DataSet.setObject(logBoxNew.text, forKey: "itemLog")



    if ((singleLogItem) != nil){ //data already available

        var newMutableListL:NSMutableArray = NSMutableArray();

        for dictL:AnyObject in singleLogItem!{


            newMutableListL.addObject(dictL as! NSDictionary)

        }


        userDefaultsL.removeObjectForKey("itemList")
        newMutableListL.addObject(DataSet)
        userDefaultsL.setObject(newMutableListL, forKey: "itemList")

    }else{ //first opponentz item
        userDefaultsL.removeObjectForKey("itemList")
        singleLogItem = NSMutableArray()
        singleLogItem!.addObject(DataSet)
        userDefaultsL.setObject(singleLogItem, forKey: "itemList")

    }

    userDefaultsL.synchronize()

//    let userDefaultsL:NSUserDefaults = NSUserDefaults.standardUserDefaults()

    let itemListFromUserDefaultsL:NSMutableArray? = userDefaultsL.objectForKey("itemList") as? NSMutableArray

    if (itemListFromUserDefaultsL != nil){
        logItems = itemListFromUserDefaultsL!
    }

    self.logTable.reloadData()

}



}

1 个答案:

答案 0 :(得分:0)

在致电logItems之前,您必须对[tableView reloadData]中的文字进行排序:

这是用Objective-C编写的示例代码,因为我不熟悉Swift。

[self.logItems sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
    NSDictionary *dict1 = obj1;
    NSDictionary *dict2 = obj2;
    NSString *text1 = [dict1 objectForKey:@"itemLog"];
    NSString *text2 = [dict2 objectForKey:@"itemLog"];
    return [text1 compare:text2];
}];