我有一个工作项目来解析.rss文件中的XML数据。我能够刷新tableview(拉动刷新)并显示新数据(如果可用)。
我想知道如何让用户知道帖子是否是新的,所以它以前从未出现在tableview中。我需要这个用新的标题和描述(如果有的话)向用户发送推送消息(每15分钟检查一次)。
该项目基本上是关于警察/救护车的监控。
Click here to see what the app looks like
感谢您帮助我:)。
我的解析XML文件的代码:
func getDataTableViewRss(){
let url:NSURL = NSURL(string: "http://semmikabel.nl/feed.rss")!
parser = XMLParser(contentsOf: url as URL)!
parser.delegate = self
parser.parse()
self.tabBarController?.tabBar.items?[0].badgeValue = String(describing: self.blogPosts.count)
}
这是我解析数据的代码:
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
eName = elementName
if elementName == "item" {
postTitle = String()
postDescription = String()
postDatePosted = String()
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
let data = string.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
if (!data.isEmpty) {
if eName == "description" {
postTitle += data
} else if eName == "title" {
postDescription += data
} else if eName == "link" {
postLink += data
} else if eName == "pubDate" {
//postDatePosted += data
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd LLL yyyy HH:mm:ss Z"
dateFormatter.locale = Locale(identifier: "en-us")
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")
let fd = dateFormatter.date(from: data)
if let fd = fd {
let formatStr = DateFormatter.dateFormat(fromTemplate: "E d MMM HH:mm", options: 0, locale: Locale.current)
dateFormatter.dateFormat = formatStr
postDatePosted += dateFormatter.string(from: fd)
}
}
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "item" {
let blogPost: BlogPost = BlogPost()
blogPost.postTitle = postTitle
blogPost.postDescription = postDescription
blogPost.postLink = postLink
savedTitle = blogPost.postTitle
blogPost.postDatePosted = postDatePosted
blogPosts.append(blogPost)
//print(blogPost.postTitle)
}
}
将每行放入tableview的代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! TableViewCell
let blogPost: BlogPost = blogPosts[indexPath.row]
cell.lblOne?.text = blogPost.postTitle
cell.lblTwo?.text = blogPost.postDescription
cell.txtTimeDate?.text = blogPost.postDatePosted
return cell
}