Ios Swift:Can';不知道如何保存用户离开tableview的方式
使用Swift 3 我已经在一个博客阅读器应用程序上工作了一段时间,但我一直在思考如何保存tableview,就像用户离开它时一样 我将数据输入mysql数据库,该数据库使用php发送,使用json和swift 3接收,以填充tableview。我希望每天都能更新这个数据库。tableview中有两个部分,顶部一个是FollowDarray,第二个是mainArray,它是所有对象在jsonArray中之后所在的位置。当用户单击follow按钮时,单个单元格将移动到FollowDarray中,用户可以在其中看到正在更新的博客并接收通知 我曾尝试使用UserDefaults将整个数组保存到一个对象中,但没有成功,因为在不需要保存整个数组的情况下,使用NSCoding创建NSData对象是一种复杂的方法(我猜) 我的问题是,有没有一种简单的方法可以保存用户离开tableview的方式? 例如:在“所有对象部分”中,假设我有3个单元格,用户单击2个单元格上的“跟随”按钮,这2个单元格现在位于“跟随部分”,1个单元格位于“所有对象部分”。如何保存此tableview的状态。我想保存跟随单元格现在所在的区域,以及更改后的按钮,该按钮现在是跟随按钮而不是跟随按钮(在隐藏一个按钮以显示另一个按钮时保存隐藏的启用代码) 我是使用UserDefaults还是CoreData?也许是CocoaPod图书馆?我是保存整个数组还是只保存其中的哪个部分以及.hidden代码?更新和添加新对象将如何影响它 我是Swift的新手,如果你能帮忙,我将不胜感激。任何你需要了解应用程序如何工作的代码,只要问一下就可以了,因为我不知道需要显示什么代码。我知道你不应该要求代码,但如果你可以,这将是伟大的,因为我已经尝试了几个月没有任何进展。谢谢大家! 以下是我如何处理传入的json数据: 斯威夫特Ios Swift:Can';不知道如何保存用户离开tableview的方式,ios,swift,uitableview,core-data,nsuserdefaults,Ios,Swift,Uitableview,Core Data,Nsuserdefaults,使用Swift 3 我已经在一个博客阅读器应用程序上工作了一段时间,但我一直在思考如何保存tableview,就像用户离开它时一样 我将数据输入mysql数据库,该数据库使用php发送,使用json和swift 3接收,以填充tableview。我希望每天都能更新这个数据库。tableview中有两个部分,顶部一个是FollowDarray,第二个是mainArray,它是所有对象在jsonArray中之后所在的位置。当用户单击follow按钮时,单个单元格将移动到FollowDarray中,用
var jsonArray: NSMutableArray = []
var mainArray = [Blog]()
var followedArray = [Blog]()
var filteredArray = [Blog]()
// Title for Header
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if !(searchController.isActive && searchController.searchBar.text != "") {
if section == 0 {
return "Followed Blogs"
}
else {
return "All Blogs"
}
}
return "Filtered Blogs"
}
// Number of Rows in Section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !(searchController.isActive && searchController.searchBar.text != "") {
if section == 0 {
return followedArray.count
}
else if (section == 1) {
return mainArray.count
}
}
return filteredArray.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let CellIdentifier = "Cell"
var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell
if cell != cell {
cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
}
// Configuring the cell
var blogObject: Blog
if !(searchController.isActive && searchController.searchBar.text != "") {
if indexPath.section == 0 {
blogObject = followedArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self)
}
else if indexPath.section == 1 {
blogObject = mainArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
}
else {
blogObject = filteredArray[indexPath.row]
cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
}
return cell
}
@IBAction func followButtonClick(_ sender: UIButton!) {
// Adding row to tag
let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {
// Showing Status Labels
let cell = self.myTableView.cellForRow(at: indexPath) as! CustomCell
cell.firstStatusLabel.isHidden = false
cell.secondStatusLabel.isHidden = false
// Change Follow to Following
(sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal)
cell.followButton.isHidden = true
cell.followedButton.isHidden = false
// Checking wether to import from mainArray or filteredArray to followedArray
if !(searchController.isActive && searchController.searchBar.text != "") {
self.myTableView.beginUpdates()
// ----- Inserting Cell to followedArray -----
followedArray.insert(mainArray[indexPath.row], at: 0)
myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)
// ----- Removing Cell from mainArray -----
mainArray.remove(at: indexPath.row)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)
self.myTableView.endUpdates()
myTableView.reloadData()
}
else {
self.myTableView.beginUpdates()
// ----- Inserting Cell to followedArray -----
let blogObject: Blog = filteredArray[indexPath.row]
let indexOfObjectInArray = mainArray.index(of: blogObject)
followedArray.insert(blogObject, at: 0)
// ----- Removing Cell from filteredArray -----
filteredArray.remove(at: indexPath.row)
blogArray.remove(at: indexOfObjectInArray!)
let rowToRemove = indexPath.row
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)
self.myTableView.endUpdates()
myTableView.reloadData()
}
}
}
func retrieveDataFromServer() {
let getDataURL = "http://exampleblog.com/receiving.php"
let url: NSURL = NSURL(string: getDataURL)!
do {
let data: Data = try Data(contentsOf: url as URL)
jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray
// Looping through jsonArray
for i in 0..<jsonArray.count {
// Create Blog Object
let bID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String
let bName: String = (jsonArray[i] as AnyObject).object(forKey: "blogName") as! String
let bStatus1: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus1") as! String
let bStatus2: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus2") as! String
let bURL: String = (jsonArray[i] as AnyObject).object(forKey: "blogURL") as! String
// New
let bType: String = (jsonArray[i] as AnyObject).object(forKey: "blogType") as! String
let bDate: String = (jsonArray[i] as AnyObject).object(forKey: "blogDate") as! String
let bPop: String = (jsonArray[i] as AnyObject).object(forKey: "blogPop") as! String
// Add Blog Objects to mainArray
mainArray.append(Blog(blogName: bName, andBlogStatus1: bStatus1, andBlogStatus2: bStatus2, andBlogURL: bURL, andBlogID: bID, andBlogType: bType, andBlogDate: bDate, andBlogPop: bPop))
}
}
catch {
print("Error: (Retrieving Data)")
}
myTableView.reloadData()
}
Blog.swift
class Blog: NSObject {
// Strings
var blogName: String?
var blogStatus1: String?
var blogStatus2: String?
var blogURL: String?
var blogID: String?
var blogType: String?
var blogDate: String?
var blogPop: String?
override init() {
}
// Converting Strings into Objects
init(blogName bName: String,
andBlogStatus1 bStatus1: String,
andBlogStatus2 bStatus2: String,
andBlogURL bURL: String,
andBlogID bID: String,
andBlogType bType: String,
andBlogDate bDate: String,
andBlogPop bPop: String)
{
super.init()
self.blogName = bName
self.blogStatus1 = bStatus1
self.blogStatus2 = bStatus2
self.blogURL = bURL
self.blogID = bID
self.blogType = bType
self.blogDate = bDate
self.blogPop = bPop
}
}
有很多不同的方法来实现您想要的,既可以本地存储信息,也可以远程存储信息。如果博客ID是唯一的,则可以将其作为字符串数组存储在默认值中:
UserDefaults.standard.set(followedBlogIds, forKey: "followedBlogIds")
然后,您可以随时抓取它们,只需确保适当地展开/投射它们:
if let storedBlogIds = UserDefaults.standard.object(forKey: "followedBlogIds") as? [String] {
//filter your blogs by id and put them in the appropriate
//arrays for your table
}
我尝试过这样做,但是UserDefault只允许使用常规数组,而不是我在mainArray中使用的那种数组,或者我错了吗?你是对的-忘记了你仅限于plist项!我猜你确实需要使用NSCoding路径来使用默认值直接存储博客。使用用户DefaultsThat smart使用blogIds使用不同的解决方案进行编辑,它们是唯一的。如何使用ID显示阵列。我该怎么做呢?在retrieveDataFromServer方法中,只需检查博客id是否与任何存储的id匹配。如果是,则将其附加到followDarray而不是主数组
if let storedBlogIds = UserDefaults.standard.object(forKey: "followedBlogIds") as? [String] {
//filter your blogs by id and put them in the appropriate
//arrays for your table
}