ios - 无法反转表格 View

标签 ios swift parse-platform

我尝试通过反转数组来反转 View 确实加载方法中的 TableView ,但它不起作用。我正在尝试做到这一点,以便它从创建提要时开始对提要进行从上到下的排序。我做错了吗?如果是这样我该怎么办?

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
}

}

最佳答案

请注意,在 View 的生命周期中,viewDidAppear 在 vi​​ewDidLoad 方法之后被调用。看来您尝试从 viewDidAppear 方法中重新加载内容,并且清除了之前在 viewDidLoad 中反转的那些数组。这就是为什么您看不到任何效果的原因。您可能想在那里反转您的数组。

关于ios - 无法反转表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32430869/

相关文章:

ios - 无法在 Xcode 中查看配置文件

iphone - UITableViewController 不加载数据

ios - 在没有核心位置的情况下获取用户位置(谷歌地图地理定位?)

ios - 在 StoryBoard 上 UILabel 上的奇怪行为\n

swift - SwiftUI 中的旋转木马/侧边栏列表样式

javascript - 从返回的查询数组中获取对象的值 (Parse.com + Node.js)

java - 使用Eclipse设置解析

ios - 根据其包含的 UITextView 调整 UITableViewCell 的大小

ios - 首次应用约束时出现意外行为

ios - 从 SceneKit 导出 USDZ 会导致模型变暗