删除uitableView中的行时,ios应用程序崩溃

时间:2018-10-01 12:02:09

标签: ios swift uitableview

我的UITableView中有一个用<div id="grandparentImage"> <div id="parentOverlay"> <div id="childCropper"></div> </div> </div>填充的问题,一切正常:插入行和节,删除行和节,重新加载。 但是有时,当我尝试以快速方式删除很多行时(通过在表格中滑动行并点击(-)来逐行删除)。它会导致崩溃,如屏幕截图所示。

该问题很难在开发应用中重现。但我的客户仍在报告。我的客户是专业人士(不是普通用户),希望能够快速使用中,大型数据。

enter image description here

这是我删除行的函数:

data: [(gamme: String, [(product: String, quantity: Double)])]

为什么会这样?

这是崩溃的xcode组织者屏幕

enter image description here 编辑:

检查 override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .destructive, title: "-") { (action, indexPath) in let cmd = self.groupedData[indexPath.section].1.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .right) self.delegate?.didDeleteCmdLine(cmd) if self.groupedData[indexPath.section].1.count == 0 { self.groupedData.remove(at: indexPath.section) tableView.deleteSections(IndexSet(integer: indexPath.section), with: UITableViewRowAnimation.right) } } return [delete] } 是否被@Reinhard建议的主线程以外的任何线程访问:

groupedData

但是private var xgroupedData = [(gamme: GammePrdCnsPrcpl, [cmdline])]() private var groupedData: [(gamme: GammePrdCnsPrcpl, [cmdline])] { get { if !Thread.isMainThread { fatalError("getting from not from main") } return xgroupedData } set { if !Thread.isMainThread { fatalError("setting from not from main") } xgroupedData = newValue } } 变量只能从主线程访问

7 个答案:

答案 0 :(得分:5)

tableView.beginUpdates()
self.tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()

答案 1 :(得分:2)

JoãoLuiz Fernandes答案的变化。...尝试

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
                if editingStyle == .delete {
                    objects.remove(at: indexPath.row)
                    tableView.deleteRows(at: [indexPath], with: .fade)
                } else if editingStyle == .insert {
                    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
                }
            }

引用(快速入侵https://www.hackingwithswift.com/example-code/uikit/how-to-swipe-to-delete-uitableviewcells

答案 2 :(得分:0)

尝试使用类似此功能的

screen_name_list = ['@x']

for name in screen_name_list:
    user = api.get_user(name)

    #initialize a list to hold all the tweepy Tweets
    alltweets = []  

    #make initial request for most recent tweets (200 is the maximum allowed count)
    new_tweets = api.user_timeline(screen_name = name, count = 200,tweet_mode='extended', include_rts=True)

    #save most recent tweets
    alltweets.extend(new_tweets)

    #save the id of the oldest tweet less one
    oldest = alltweets[-1].id - 1

    #keep grabbing tweets until there are no tweets left to grab
    while len(new_tweets) > 0:
      print 'getting tweets before %s' % (oldest)

        #all subsiquent requests use the max_id param to prevent duplicates
        new_tweets = api.user_timeline(screen_name = name, count=200, max_id=oldest, tweet_mode='extended')

        #save most recent tweets
        alltweets.extend(new_tweets)

        #update the id of the oldest tweet less one
        oldest = alltweets[-1].id - 1

        print "...%s tweets downloaded so far" % (len(alltweets))

    #transform the tweepy tweets into a 2D array that will populate the csv 
    outtweets = [[tweet.id_str, tweet.created_at, tweet.full_text.encode('utf-8')] for tweet in alltweets]
    tweet_time = [index[1] for index in outtweets]
    tweet_list = [index[2] for index in outtweets]

答案 3 :(得分:0)

你可以尝试

         var myDataArray = ["one","two","three"]
         //wanna insert a row then in button action or any action write
         myDataArray.append("four")
         self.tblView.reloadData()
         // wanna delete a row
         myDataArray.removeObject("two")
         // you can remove data at any specific index liek
         //myDataArray.remove(at: 2)
         self.tblView.reloadData()

答案 4 :(得分:0)

用户点击删除按钮后,您将从数据源groupedData(是一个数组)中删除相应的行和(如果这是该节的最后一行)相应的节。但是,数组操作不是线程安全的
可能是另一个线程正在使用该数组,而删除操作对其进行了修改吗? 在这种情况下,应用程序可能会崩溃。当在短时间内触发多个动作时,危险当然更高,正如您所描述的那样。
避免多线程问题的一种方法(也许不是最好的方法)是仅在主线程上访问数组。
如果这会减慢主线程的速度,则可以使用一个同步数组,该数组允许同时进行多个读取,但是只有一个写入会阻止所有读取,请参见here

答案 5 :(得分:0)

只有一个更新@codeBy他们的答案。

请在删除特定行时更新数据源文件。

tableView.beginUpdates()
self.whatEverDataSource.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()

这将导致您的数据源也与TableView同时更新。发生崩溃的原因可能是由于未更新数据源。

答案 6 :(得分:0)

在删除节中的最后一项时,您可以尝试一次删除该节,而不是一次删除该行,然后删除它所属的节吗?

template<typename T, int N, int M>
class matrix {
    T vals[N][M] = {};

public:
    matrix() {
        //sth
    }

    matrix<T, N, M> operator*(matrix<T, N, M> r) {
        return matrix<T, N, M>{};
    }
};
相关问题