gpt4 book ai didi

ios - Swift 4 - 将新行从数组插入到只有一个静态行的部分

转载 作者:行者123 更新时间:2023-11-30 11:29:26 25 4
gpt4 key购买 nike

我的 tableView 在开始时有 n 个部分。每个部分只有一行,但每一行都包含添加到数组中的部分行的按钮。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
let postModel = self.postsArray[section];

if (self.currentSection == section)
{
return 1 + (postModel.comments?.count)!;
}
else
{
print("nie rozszerzone")

return 1;
}
}

func numberOfSections(in tableView: UITableView) -> Int
{
return self.postsArray.count;
}

我尝试这样做

func showComments(model: MGPostModel, sender: MGCommunityPostTableViewCell)
{
guard let path = self.communityTableView.indexPath(for: sender) else {
return;
}

print(path)
print(model.comments?.count as Any)

self.currentSection = path.section;
//model.comments.count is equal 3
let indexPath = IndexPath(row: (model.comments?.count)!, section: path.section);

print(indexPath)

self.communityTableView.beginUpdates();
self.communityTableView.insertRows(at: [indexPath], with: .automatic);
self.communityTableView.endUpdates();

//self.communityTableView.reloadRows(at: [indexPath], with: .automatic);
}

但响应是“无效更新:第 0 节中的行数无效。更新 (4) 后现有节中包含的行数必须等于更新前该节中包含的行数 ( 1),加上或减去从该部分插入或删除的行数(1 插入,0 删除)..."

我知道 numberOfRows 期望 4 行,但为什么 insertRows 在应该插入 3 行时尝试只插入 1 行?

最佳答案

第 1 步:- 您必须更新 tableview 方法中使用的行数数据。前->myarray.count
postModel.comments?.count --> 在插入行之前更新它。

第 2 步:-更新行数数据后,您可以进行更新、添加或删除行或重新加载表格 View 。

注意:在您的代码中没有任何用于声明表格 View 中的行数的数据更新。

关于ios - Swift 4 - 将新行从数组插入到只有一个静态行的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50406539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com