Ios 如何在Swift 4中添加新单元格和新标题而不重新加载表格?
我已经看到了很多关于我的问题的答案,但没有一个能解决我的问题。我在这里提出一个新问题 我有一个API,它返回数据。我在标题和单元格中显示它。如果API返回has\u more key的Ios 如何在Swift 4中添加新单元格和新标题而不重新加载表格?,ios,swift,uitableview,Ios,Swift,Uitableview,我已经看到了很多关于我的问题的答案,但没有一个能解决我的问题。我在这里提出一个新问题 我有一个API,它返回数据。我在标题和单元格中显示它。如果API返回has\u more key的1,则还有一个load more按钮。因此,当我点击LoadMore按钮时,我再次调用该API,以便获得剩余数据,但它也会刷新整个表视图,因为我重新加载了表视图 现在,我想做的是在不重新加载现有表视图的情况下附加新的单元格和标题,这样就不会影响表视图中以前的标题和单元格 这是API函数: func getOvera
1,则还有一个load more按钮。因此,当我点击LoadMore按钮时,我再次调用该API,以便获得剩余数据,但它也会刷新整个表视图,因为我重新加载了表视图
现在,我想做的是在不重新加载现有表视图的情况下附加新的单元格和标题,这样就不会影响表视图中以前的标题和单元格
这是API函数:
func getOverallWinners(has: Int)
{
let params = ["api_token": Constants.USER_INFO["api_token"].rawValue,"has_more": has]
ServiceHelper.sharedInstance.sendRequest(path: "contest-overall-winning", params: params, showSpinner: true)
{ (response, error) in
if error != nil
{
print("Error: \(error.debugDescription)")
}
else
{
self.winnerArr = response["result"].arrayObject as? Array<Dictionary<String,Any>>
self.overallL.text = "$\(String(describing: response["overall"].rawString()!))"
self.winningTbl.beginUpdates()
let indexPath:IndexPath = IndexPath(row:((self.winnerArr?.count)! - 1), section:0)
self.winningTbl.insertRows(at: [indexPath], with: .left)
self.winningTbl.endUpdates()
self.winningTbl.scrollToRow(at: indexPath, at: .bottom, animated: true)
let loadMore = response["has_more"].rawValue as? Int
if loadMore == 0
{
self.constantHeight4LoadMore.constant = 0
}
else
{
self.constantHeight4LoadMore.constant = 40
}
}
}
}
func getOverallWinners(has:Int)
{
让params=[“api_令牌”:常量。用户_信息[“api_令牌”]。rawValue,“has_more”:has]
ServiceHelper.sharedInstance.sendRequest(路径:“竞赛总冠军”,参数:params,showSpinner:true)
{(响应、错误)在
如果错误!=nil
{
打印(“错误:\(Error.debugDescription)”)
}
其他的
{
self.winnerArr=响应[“结果”]。arrayObject作为?数组
self.overall.text=“$\(字符串(描述:响应[“总体”].rawString()!)”
self.winningbl.beginUpdate()
让indexPath:indexPath=indexPath(行:((self.winnerArr?.count)!-1),节:0)
self.winningbl.insertRows(位于:[indepath],带:。左)
self.winningbl.endUpdates()
self.winningbl.scrollToRow(at:indepath,at:.底部,动画:true)
让loadMore=response[“has_more”]。rawValue为?Int
如果loadMore==0
{
self.constantHeight4LoadMore.constant=0
}
其他的
{
self.constantHeight4LoadMore.constant=40
}
}
}
}
以下是te tableView委托:
func numberOfSections(in tableView: UITableView) -> Int {
return (winnerArr?.count)!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let rowArray = winnerArr![section]["response"] as? Array<Dictionary<String,Any>>
return rowArray!.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 60
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
self.prizeArr.removeAll()
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: cellReuseIdentifier) as! OverallHeaderCell
let resArr = self.winnerArr![section]["response"]! as? Array<Dictionary<String,Any>>
for prize in resArr!
{
let doubleStr = prize["prize"] as? NSString
self.prizeArr.append((doubleStr?.doubleValue)!)
}
let sumedStr = prizeArr.reduce(0, +)
header.textL[0].text = winnerArr![section]["name"] as? String
header.textL[1].text = "$\(sumedStr)"
header.textL[3].text = "\(String(describing: resArr!.count))"
return header
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "owCell", for: indexPath) as! OWCell
let response = winnerArr![indexPath.section]["response"] as? Array<Dictionary<String,Any>>
cell.collectionL[0].text = (response![indexPath.row]["name"] as! String)
cell.collectionL[1].text = "$\(String(describing:response![indexPath.row]["prize"]!))"
cell.collectionL[2].text = "WIN"
cell.selectionStyle = .none
return cell
}
func numberOfSections(在tableView:UITableView中)->Int{
返回(winnerArr?.count)!
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
让rowArray=winnerArr![section][“response”]as?数组
返回rowArray!.count
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
返回60
}
func tableView(u tableView:UITableView,heightForHeaderInSection:Int)->CGFloat{
返回60
}
func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView?
{
self.prizeArr.removeAll()
让header=tableView.dequeueReusableHeaderFooterView(标识符为cellReuseIdentifier)作为!总体HeaderCell
让resArr=self.winnerArr![section][“response”]!as?数组
为了在resArr中获得奖品!
{
让doubleStr=prize[“prize”]作为?NSString
self.prizeArr.append((doubleStr?doubleValue)!)
}
设sumedStr=prizeArr.reduce(0,+)
header.textL[0]。text=winnerArr![section][“name”]作为?字符串
header.textL[1]。text=“$\(sumedStr)”
header.textL[3].text=“\(字符串(描述:resArr!.count))”
回流集管
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“owCell”,for:indexPath)作为!owCell
让response=winnerArr![indexPath.section][“response”]as?数组
cell.collectionL[0]。text=(响应![indexPath.row][“name”]as!String)
cell.collectionL[1].text=“$\(字符串(描述:响应![indexPath.row][“prize”]!)
cell.collectionL[2]。text=“WIN”
cell.selectionStyle=.none
返回单元
}
我尝试了上面的beginUpdate()
,但没有任何效果,因为每次单击“加载更多”按钮,我都会收到新的标题。在这些标题中会有新的单元格。有人能帮忙吗
这不是重复的,因为解决方案是在节中插入新行,但这里我必须首先创建新节,然后在其中添加新行。我不想触碰旧的部分。insertRows
和insertSections
是实现您想要的功能。什么具体不起作用?可能是@mag_zbc的重复它抛出此错误由于未捕获的异常“NSRangeException”终止应用程序,原因:“***-[\uu nsarray objectAtIndexedSubscript::]:索引0超出空数组的边界”
此错误发生在哪里?它每次都会在终端中向应用程序委托抛出我,我得到这个错误。