0

I tried to reverse my table view in the view did load method by reversing the arrays but it's not working. I'm trying to make it so that it sorts the feed from the time it was created from top to bottom. Am I doing it wrong? If so what should I do?

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
}

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

0

Observe that in the view's life cycle the viewDidAppear gets called after your viewDidLoad method. It appears that you try to reload the contents from within your viewDidAppear method, and there you are clearing those arrays which you had reversed earlier in viewDidLoad. This is the reason why you are not seeing any effect. You might want to reverse your arrays there.

Shripada
  • 6,296
  • 1
  • 30
  • 30
0

You are reversing array in viewDidLoad, but you append something in viewDidAppear. viewDidAppear call after viewDidLoad. You should reverse array in the end of viewDidAppear before call 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()
        }
    })
}
tuledev
  • 10,177
  • 4
  • 29
  • 49
0

I figured it out what I did was I did this instead of append...

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

What this basically does is appends the newest message to the beginning of the array.