2015-09-07 59 views
0

我試圖顛倒我的表視圖在視圖的負載方法通過顛倒數組,但它不工作。我試圖將它從上到下的創建時間排序。我做錯了嗎?如果是這樣,我該怎麼辦?不能反轉表視圖

class Feed: UIViewController, UITableViewDelegate, UITableViewDataSource { 

@IBOutlet var table: UITableView! 

@IBOutlet var feedBar: UINavigationBar! 

var titles = [String]() 
var messages = [String]() 
var usernames = [String]() 
var types = [String]() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    table.dataSource = self 
    messages.reverse() 
    titles.reverse() 
    usernames.reverse() 
    types.reverse() 
} 

func UIColorFromRGB(rgbValue: UInt) -> UIColor { 
    return UIColor(
     red: CGFloat((rgbValue & 0xFF0000) >> 16)/255.0, 
     green: CGFloat((rgbValue & 0x00FF00) >> 8)/255.0, 
     blue: CGFloat(rgbValue & 0x0000FF)/255.0, 
     alpha: CGFloat(1.0) 
    ) 
} 


override func viewDidAppear(animated: Bool) { 
    println("View appeared") 

    self.messages.removeAll(keepCapacity: true) 
    self.titles.removeAll(keepCapacity: true) 
    self.usernames.removeAll(keepCapacity: true) 
    self.types.removeAll(keepCapacity: true) 

    var postQuery = PFQuery(className: "Post") 

    postQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 

     if let objects = objects { 

      for object in objects { 

       self.messages.append(object["message"] as! String) 

       self.titles.append(object["title"] as! String) 

       self.usernames.append(object["username"] as! String) 

       self.types.append(object["type"] as! String) 

       self.table.reloadData() 


      } 

     } 

    }) 
} 


func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let postCell = tableView.dequeueReusableCellWithIdentifier("feedPost", forIndexPath: indexPath) as! PostCell 

    var post = PFObject(className: "Post") 

    if messages.isEmpty == false { 
     postCell.message.text = messages[indexPath.row] 
    } 

    if titles.isEmpty == false { 
     postCell.postTtitle.text = titles[indexPath.row] 
    } 

    if usernames.isEmpty == false { 
     postCell.posterName.setTitle(usernames[indexPath.row], forState: .Normal) 
    } 

    if self.types[indexPath.row] == "post1" { 
     postCell.sideLeft.backgroundColor = self.UIColorFromRGB(0xFCFFBD) 
     postCell.sideRight.backgroundColor = self.UIColorFromRGB(0xFCFFBD) 

    } else if self.types[indexPath.row] == "post2" { 
     postCell.sideLeft.backgroundColor = self.UIColorFromRGB(0xFFB4AC) 
     postCell.sideRight.backgroundColor = self.UIColorFromRGB(0xFFB4AC) 

    } else if self.types[indexPath.row] == "post3" { 
     postCell.sideLeft.backgroundColor = self.UIColorFromRGB(0xFFD5A4) 
     postCell.sideRight.backgroundColor = self.UIColorFromRGB(0xFFD5A4) 
    } 


    postCell.selectionStyle = .None 

    postCell.message.scrollRangeToVisible(NSMakeRange(0, 0)) 

    return postCell 

} 

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

} 
+0

這東西可能會幫助你,它的目標C.但你會得到主意https://github.com/rcmcastro/whatsapp-ios –

回答

0

觀察到在視圖的生命週期中viewDidAppear在viewDidLoad方法後被調用。看起來您嘗試從viewDidAppear方法中重新加載內容,並且在那裏清除了您之前在viewDidLoad中反轉的那些數組。這就是你沒有看到任何效果的原因。您可能想要在那裏反轉陣列。

+0

是的,我試過這個,它沒有工作。不知道爲什麼。 –

+0

您是否看到數組在postQuery.findObjectsInBackgroundWithBlock中被填充? – Shripada

+0

是的,我只是想讓它像其他社交媒體Feed一樣以其他方式填充。 –

0

您正在倒置viewDidLoad中的數組,但您在viewDidAppear中追加了一些內容。 後致電viewDidAppear。你應該在viewDidAppear年底扭轉數組調用之前self.table.reloadData()

override func viewDidAppear(animated: Bool) { 
    println("View appeared") 

    self.messages.removeAll(keepCapacity: true) 
    self.titles.removeAll(keepCapacity: true) 
    self.usernames.removeAll(keepCapacity: true) 
    self.types.removeAll(keepCapacity: true) 

    var postQuery = PFQuery(className: "Post") 

    postQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 

     if let objects = objects { 

      for object in objects { 

       self.messages.append(object["message"] as! String) 

       self.titles.append(object["title"] as! String) 

       self.usernames.append(object["username"] as! String) 

       self.types.append(object["type"] as! String) 




      } 
      messages.reverse() 
      titles.reverse() 
      usernames.reverse() 
      types.reverse() 
      self.table.reloadData() 
     } 
    }) 
} 
+0

我剛試過這個,它沒有奏效。我不知道爲什麼。 –

+0

你能打印出'messages'嗎? – anhtu

+0

我應該在cellForRowAtIndexPath方法中打印它還是視圖確實出現了方法? –

0

我想通了什麼,我所做的就是我這樣做,而不是追加...

self.messages.insert(object["message"] as! String, atIndex: 0) 

什麼這通常做的就是追加到數組開頭的最新消息。