Swift:如何从表视图中删除重复项?

2024-01-01

这里的问题是我的表视图中出现重复项,我知道为什么,但我不知道如何修复它并实现不同的系统。

我的应用程序是一个博客阅读器,它使用 PHP 从 MYSQL 数据库读取数据,然后将 JSON 发送到我的 Swift 应用程序。我的表视图有两个部分,第一部分用于数据库中的所有对象,第二部分用于当我单击单元格上的“跟随”按钮时,基本上将对象从 mainArray 移动到 followedArray。每个部分都使用一个数组,例如,我将所有对象从 mainArray 移动到 followArray,然后更新表,我在 mainArray 中再次获取所有这些对象,显然是因为 mainarray 是空的,并且代码只是在执行其工作。

那么,我怎样才能实现一个更好的系统,这样当用户将对象从一个部分移动到另一个部分(或从 mainArray 到 followArray)时,mainArray 就不会重新填充它拥有且现在位于 followArray 中的相同对象。

这是我使用的代码。

MainController.swift - Tableview 所在的类

var mainArray = [Blog]()
var followedArray = [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
}

// CellForRowAt indexPath
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
}

// Follow Button
@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()

            // After Updating Table, Save the Archived to UserDefaults
            saveUserDefaults()
        }
        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)
            mainArray.remove(at: indexOfObjectInArray!)
            let rowToRemove = indexPath.row
            self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)

            self.myTableView.endUpdates()

            myTableView.reloadData()

            // After Updating Table, Save the Archived to UserDefaults
            saveUserDefaults()
        }
    }
}


// Retrieving Data from Server
func retrieveDataFromServer() {

    let getDataURL = "http://example.com/receiving.php"
    let url: NSURL = NSURL(string: getDataURL)!

    do {
        let data: Data = try Data(contentsOf: url as URL)
        let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray

        // Looping through jsonArray
        for jsonObject in jsonArray {

            if let blog = Blog.createGame(from: jsonObject as AnyObject) {

                mainArray.append(blog)
            }
        }
    }
    catch {
        print("Error: (Retrieving Data)")
    }
    myTableView.reloadData()
}

Blog.swift - 处理我也使用 NSCoder 的博客对象

import UIKit

class Blog: NSObject, NSCoding {

var blogName: String!
var blogStatus1: String!
var blogStatus2: String!
var blogURL: String!
var blogID: String!
var blogType: String!
var blogDate: String!
var blogPop: String!

static func createBlog(from jsonObject: AnyObject) -> Blog? {

    guard let bID: String = jsonObject.object(forKey: "id") as? String,
        let bName: String = jsonObject.object(forKey: "blogName") as? String,
        let bStatus1: String = jsonObject.object(forKey: "blogStatus1") as? String,
        let bStatus2: String = jsonObject.object(forKey: "blogStatus2") as? String,
        let bURL: String = jsonObject.object(forKey: "blogURL") as? String,
        let bType: String = jsonObject.object(forKey: "blogType") as? String,
        let bDate: String = jsonObject.object(forKey: "blogDate") as? String,
        let bPop: String = jsonObject.object(forKey: "blogPop") as? String

        else {
          print("Error: (Creating Blog Object)")
          return nil
}

let blog = Blog()
    blog.blogName = bName
    blog.blogStatus1 = bStatus1
    blog.blogStatus2 = bStatus2
    blog.blogURL = bURL
    blog.blogID = bID
    blog.blogType = bType
    blog.blogDate = bDate
    blog.blogPop = bPop
    return blog
}

convenience required init?(coder aDecoder: NSCoder) {
    self.init ()
    self.blogName = aDecoder.decodeObject(forKey: "blogName") as! String
    self.blogStatus1 = aDecoder.decodeObject(forKey: "blogStatus1") as! String
    self.blogStatus2 = aDecoder.decodeObject(forKey: "blogStatus2") as! String
    self.blogURL = aDecoder.decodeObject(forKey: "blogURL") as! String
    self.blogID = aDecoder.decodeObject(forKey: "blogID") as! String
    self.blogType = aDecoder.decodeObject(forKey: "blogType") as! String
    self.blogDate = aDecoder.decodeObject(forKey: "blogDate") as! String
    self.blogPop = aDecoder.decodeObject(forKey: "blogPop") as! String
}

func encode(with aCoder: NSCoder) {
    aCoder.encode(blogName, forKey: "blogName")
    aCoder.encode(blogStatus1, forKey: "blogStatus1")
    aCoder.encode(blogStatus2, forKey: "blogStatus2")
    aCoder.encode(blogURL, forKey: "blogURL")
    aCoder.encode(blogID, forKey: "blogID")
    aCoder.encode(blogType, forKey: "blogType")
    aCoder.encode(blogDate, forKey: "blogDate")
    aCoder.encode(blogPop, forKey: "blogPop")
 }
}

有没有办法在重新填充 mainArray 之前进行检查,以查看 followArray 中的内容以及数据库中缺少或添加的内容以导入而不是创建重复项,因为将添加新博客并且用户将跨部分传输博客,因此这是一个主要问题我有。

感谢您的帮助,因为我仍在学习 Swift,谢谢。


我建议将以下类别中的博客的唯一标识符保存到数组中,并在每次重新加载 tableView 时,将正确的单元格移动到正确的部分。

你似乎正在使用用户默认值 https://developer.apple.com/documentation/foundation/userdefaults但对它们没有任何修改。使用我的方法,需要保存到 UserDefaults 并从 UserDefaults 加载的唯一数组是关注的博客列表。其余的默认为主列表,即使出现新博客也是如此。

你还需要一个数组:

var mainArray = [Blog]()
var followedArray = [Blog]()
var followedIdentifiers = [String]()

或标识符的任何数据类型

您还可以使用Set因为您不希望以下标识符中出现重复项

var followedIdentifiers = Set<String>()

以下是对代码相关部分的修改(我的更改标记为<----):

// Checking whether to import from mainArray or filteredArray to followedArray
if !(searchController.isActive && searchController.searchBar.text != "") {

    self.myTableView.beginUpdates()

    // Save identifier into followedIdentifier array <--------------
    self.followedIdentifiers.insert(mainArray[indexPath.row].blogID)

    // ----- 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()

    // After Updating Table, Save the Archived to UserDefaults
    saveUserDefaults()
} else {

    self.myTableView.beginUpdates()

    // Remove identifier into followedIdentifier array <------------
    self.followedIdentifiers.remove(followedArray[indexPath.row].blogID)

    // ----- 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)
    mainArray.remove(at: indexOfObjectInArray!)
    let rowToRemove = indexPath.row
    self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)

    self.myTableView.endUpdates()

    myTableView.reloadData()

    // After Updating Table, Save the Archived to UserDefaults
    saveUserDefaults()
}

// Retrieving Data from Server
func retrieveDataFromServer() {

    let getDataURL = "http://example.com/receiving.php"
    let url: NSURL = NSURL(string: getDataURL)!

    do {
        let data: Data = try Data(contentsOf: url as URL)
        let jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray

        // Clear the arrays      <-------------
        self.followedArray = [Blog]()
        self.mainArray = [Blog()]

        // Looping through jsonArray
        for jsonObject in jsonArray {

            if let blog = Blog.createBlog(from: jsonObject as AnyObject) {
                // Check if identifiers match <------------
                if followedIdentifiers.contains(blog.blogID) {
                    self.followedArray.append(blog)
                } else {
                    self.mainArray.append(blog)
                }

            }
        }
    } catch {
        print("Error: (Retrieving Data)")
    }
    myTableView.reloadData()
}

为了让它发挥作用跨会话你的里面一定有类似的东西saveUserDefaults()

UserDefaults.standard.setValue(Array(self.followedIdentifiers), forKey: "someName")

这是您从 UserDefaults 加载的位置

self.followedIdentifiers = Set(UserDefaults.standard.stringArray(forKey: "someName"))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift:如何从表视图中删除重复项? 的相关文章

  • 如何将 .ipa 转换为 .app 文件?

    每个人 我有一些 ipa 文件 想要转换为包含 app 包的存档文件 我试图这样做 但我没有办法 有什么办法可以做到这一点吗 请帮我 将 ipa 重命名为 zip 然后提取即可
  • 如何从 Xcode 4.5 卸载 iOS 5.0 模拟器

    我已经安装了 Xcode 4 5 由于我编写应用程序是为了好玩 我不打算支持旧版 iOS 版本 并且为了节省 500 GB iMac 上的空间 是否有办法删除它 尝试从 Xcode 包中支持的平台中删除 iPhone iPhoneSimul
  • supportedInterfaceOrientations 方法不会重写其超类中的任何方法

    在 UIViewController 中 这段代码 public override func supportedInterfaceOrientations gt UIInterfaceOrientationMask if let mainC
  • 更改目录时 Gitlab CI 运行程序作业失败退出状态 1

    我正在使用我的个人机器作为使用 Fastlane 的 iOS 项目的运行程序 这主要是因为共享运行器没有为 iOS 设置 因为它们没有安装 Xcode 更改目录时我的作业立即失败 它是一个 shell 运行程序 根本没有其他自定义配置 有什
  • 与 parse-server 和 auth0 的自定义身份验证集成

    我想将 auth0 com 与开源解析服务器结合使用 我当前的方法是通过 iOS 的 Lock 库使用标准登录从 auth0 获取令牌 使用该令牌 我想在解析服务器上调用自定义身份验证方法 该方法检查令牌是否有效 如果有效则将登录用户 我的
  • 在 macOS 上使用 Swift 3 从剪贴板读取

    我是 Swift 的初学者 我想弄清楚如何在 macOS Swift 3 上读取已复制到剪贴板的内容 我搜索了很多 但似乎找不到任何有效的东西 我从网上尝试过的一些事情 var pasteboardItems NSPasteboardIte
  • iPhone 的翻译器?

    我对为 iPhone 制作一个解释器很感兴趣 这将是一个实验性的想法 但可能会很棒 我喜欢让我自 己的语言适合移动计算和数学的想法 我查阅了很多资料 发现有关 iPhone 上的口译员的信息很复杂 苹果会允许什么 我见过这个应用程序 这是一
  • 在 Xcode 中查找未使用的文件

    我最近开始开发一个新应用程序 它基本上是我以前制作的应用程序的副本 但做了一些更改 为了制作这个新应用程序 我复制了旧应用程序并删除了一些不需要的内容 我想知道 有没有办法知道 Xcode 中正在使用哪些类文件 或者有什么关于如何查找未使用
  • 在 Swift 中的 For 循环中更改对象的属性

    我创建了一个名为 ShoppingList 的简单结构 struct ShoppingList var shoppingListId NSNumber var title String var groceryItems GroceryIte
  • 上下文菜单未在 SwiftUI 中更新

    我正在尝试设置 SwiftUI contextMenu带有一个切换按钮Bool价值 上下文菜单的按钮文本应该在以下情况下更改 Bool切换 但上下文菜单不会更新 有没有办法强制更新上下文菜单 描述问题的示例代码 import SwiftUI
  • 无法在 xcode 8 beta 6 上编译 AWS CustomIdentityProvider

    我在 iOS 应用程序中使用 Amazon Cognito 和 Facebook 登录 直到 beta 5 为止此代码从这个SO线程 https stackoverflow com questions 37597388 aws cognit
  • Grand Central Dispatch (GCD) 调度源标志

    我最近不再使用 to GCD 调度来源 https developer apple com documentation dispatch 1385630 dispatch source create监视文件更改 效果很好 API 也变得更加
  • “无法取消归档名为 UITableViewController 的元素”

    我一直在按照 构建你的第二个 iOS 应用程序 教程一步步进行 在教程承诺所有错误都会消失之后 我遇到了这个错误 但直到其他错误都出现后 该错误才出现 全部更正 我尝试编译它 错误 The document MainStoryboard i
  • iOS绘图3D图形库[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我正在搜索一个可以帮助我绘制 3D 图表的库 我想要类似的东西这一页 http www math uri edu bkaskosz fla
  • iOS 有 INTERNET 权限吗?

    我在 iOS 设备上的 flutter dio 包上遇到了一个奇怪的问题 我编写了一个向 url 发送 GET 请求的应用程序 Android 上一切正常 但 iOS 上的请求似乎无法通过 没有发生任何错误 什么也没有 我在 Android
  • 使用 PDFOutline 将 TOC 添加到 Swift/Cocoa 中的 PDFDocument

    我正在开发一个小程序 将多个单页 PDF 合并到一个多页 PDF 中 我正在 Swift4 MacOS Cocoa 中工作 但我一生都无法在 Swift 中找到任何类型的示例来创建大纲 仅遍历现有的大纲 我对此非常熟悉 使用对文档的最佳猜测
  • 如何将设备上未保存的图片上传到dropbox帐户?(IOS)

    Dropbox RestClient 仅保存文件 所以我想先将图像保存在本地文件夹中 然后上传它 结果它保存了文件 但它已损坏 NSString localPath NSBundle mainBundle pathForResource I
  • AdMob 和 DFP 广告联盟之间的区别?

    我正在尝试在我的 iOS 应用程序上显示横幅广告和插页式广告 但现在我对广告网络感到困惑 AdMob 与 DFP 有何不同 哪一种更适合投放广告 有人可以提供帮助吗 提前致谢 AdMob 是一个广告网络 作为发布商 您可以通过展示从网络投放
  • 找不到 Cocoa/Cocoa.h 文件

    我在用XMPPFramework在我的应用程序中 我已将 Cocoa Cocoa h 导入到我的 m 文件中 但是当我构建项目时Xcode显示错误 错误 未找到 Cocoa Cocoa h 文件 我该如何解决这个错误 如果您正在为 iOS
  • iOS 目标 c 中的 AES/CBC/PKCS5Padding 结果与 Android 不同

    我在 Android 应用程序中使用 AES CBC PKCS5Padding 代码就像 private static String TRANSFORMATION AES CBC PKCS5Padding private static St

随机推荐