Ios 无法反转表视图
我试图通过反转数组来反转view Dod load方法中的表视图,但它不起作用。我正在努力使它能够从创建提要时起从上到下对提要进行排序。我做错了吗?如果是,我该怎么办Ios 无法反转表视图,ios,swift,parse-platform,Ios,Swift,Parse Platform,我试图通过反转数组来反转view Dod load方法中的表视图,但它不起作用。我正在努力使它能够从创建提要时起从上到下对提要进行排序。我做错了吗?如果是,我该怎么办 class Feed: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var table: UITableView! @IBOutlet var feedBar: UINavigationBar! var titles = [
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
}
}
请注意,在视图的生命周期中,viewDidLoad方法之后会调用ViewDidDisplay。看起来您试图从ViewDidDisplay方法中重新加载内容,并且正在清除之前在viewDidLoad中反转的数组。这就是你没有看到任何效果的原因。您可能希望在那里反转阵列 您正在viewDidLoad中反转数组,但在ViewDidDisplay中追加了一些内容。viewDidLoad后调用ViewDidDisplay。在调用self.table.reloadData之前,您应该在viewdide的末尾反转数组
我发现我做的是做这个而不是附加
self.messages.insert(object["message"] as! String, atIndex: 0)
这基本上是在数组的开头添加最新的消息。这东西可能会对你有所帮助,在目标C中。但你会知道我刚刚尝试了这个,但它不起作用。我不知道为什么。你能打印邮件吗?我应该用CellForRowatineXpath方法还是view did Asped方法打印?ViewDidAsped和CellForRowatineXpath也是这是我得到的日志。。。[这是留言][这是留言,这是另一封留言][这是留言,这是另一封留言,这是另一封留言][这是留言,这是另一封留言,这是另一封留言][这是留言,这是另一封留言,这是另一封留言]你看到postQuery.findObjectsInBackgroundWithBlock中的数组被填充了吗?是的,我看到了,我只想让它以另一种方式填充,就像大多数社交媒体提要一样。
self.messages.insert(object["message"] as! String, atIndex: 0)