使用 SWIFT 3 的单元格不显示没有 html 标记的更新 JSON
Posted
技术标签:
【中文标题】使用 SWIFT 3 的单元格不显示没有 html 标记的更新 JSON【英文标题】:Cell not showing updated JSON without html tags with SWIFT 3 【发布时间】:2016-11-10 16:19:03 【问题描述】:我有一个项目,使用 Alamofire、Swift 和 Xcode。我有来自 web url 作为 JSON 的数据。 JSON 很好,但在我的单元格中,它仍然显示 html 标签,但是当我打印到控制台时,HTML 标签不存在。我确定我在代码中的单元级别做错了,但似乎看不到它是什么。代码如下:有帮助吗?
代码
import UIKit
import Alamofire
struct postinput
let mainImage : UIImage!
let name : String!
let author : String!
let summary : String!
class TableViewController: UITableViewController
var postsinput = [postinput]()
var mainURL = "https://www.example.com/api"
typealias JSONstandard = [String : AnyObject]
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
callAlamo(url: mainURL)
func callAlamo(url : String)
Alamofire.request(url).responseJSON(completionHandler:
response in
self.parseData(JSONData: response.data!)
)
func parseData(JSONData : Data)
do
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard
// print(readableJSON)
if let posts = readableJSON["posts"] as? [JSONstandard]
for post in posts
let title = post["title"] as! String
let author = post["author"] as! String
guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else
return
let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
print(str)
print(author)
if let imageUrl = post["image"] as? String
let mainImageURL = URL(string: imageUrl )
let mainImageData = NSData(contentsOf: mainImageURL!)
let mainImage = UIImage(data: mainImageData as! Data)
postsinput.append(postinput.init(mainImage: mainImage, name: title, author: author, summary: summary))
DispatchQueue.main.async
self.tableView.reloadData()
catch
print(error)
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
return postsinput.count
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
// cell?.textLabel?.text = titles[indexPath.row]
let mainImageView = cell?.viewWithTag(2) as! UIImageView
mainImageView.image = postsinput[indexPath.row].mainImage
//(cell?.viewWithTag(2) as! UIImageView).image = postsinput[indexPath.row].mainImage
let mainLabel = cell?.viewWithTag(1) as! UILabel
mainLabel.text = postsinput[indexPath.row].name
mainLabel.font = UIFont(name: "Helvetica", size:14)
let autLabel = cell?.viewWithTag(3) as! UILabel
autLabel.text = postsinput[indexPath.row].author
autLabel.font = UIFont(name: "Helvetica", size:12)
let sumLabel = cell?.viewWithTag(4) as! UILabel
sumLabel.text = postsinput[indexPath.row].summary
sumLabel.font = UIFont(name: "Helvetica", size:12)
//(cell?.viewWithTag(3) as! UILabel).text = postsinput[indexPath.row].author
return cell!
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
【问题讨论】:
【参考方案1】:这里改一下
let sumLabel = cell?.viewWithTag(4) as! UILabel
sumLabel.text = postsinput[indexPath.row].summary
到这里
let sumLabel = cell?.viewWithTag(4) as! UILabel
let str = postsinput[indexPath.row].summary
sumLabel.text = str.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
【讨论】:
以上是关于使用 SWIFT 3 的单元格不显示没有 html 标记的更新 JSON的主要内容,如果未能解决你的问题,请参考以下文章
为啥集合视图中的动态集合单元格不显示为给定大小并且在 swift 3 中滚动后会发生变化
iOS Swift UICollectionView 照片浏览 - 当单元格不可见时如何停止取消 DispatchQueue