Ios 在最后一节插入带有按钮的行tableview
我想制作一个分区中带有按钮的tableview。我希望按钮可以像这样在tableview中再添加一行 以下是源代码:Ios 在最后一节插入带有按钮的行tableview,ios,swift,swift3,Ios,Swift,Swift3,我想制作一个分区中带有按钮的tableview。我希望按钮可以像这样在tableview中再添加一行 以下是源代码: func numberOfSections(in tableView: UITableView) -> Int { return 2 } func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int { if sectionInd ==
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection sectionInd: Int) -> Int {
if sectionInd == 0 {
return others.count
} else {
return 1
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ShareCell", for: indexPath as IndexPath) as! SelectOthersTableViewCell
cell.firstName.text = others[indexPath.row].firstname
cell.lastName.text = others[indexPath.row].lastname
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "addCell", for: indexPath as IndexPath) as! addTableCell
cell.addCells.tag = indexPath.row
cell.addCells.addTarget(self, action: #selector(OthersViewController.addButtonClicked(sender:)), for: UIControlEvents.touchUpInside)
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
var height:CGFloat = CGFloat()
if indexPath.section == 0 {
height = 145
} else {
height = 50
}
return height
}
@objc func addButtonClicked(sender:UIButton) {
data.append("Guest 1")
let buttonPosition = sender.convert(CGPoint.zero, to: self.tableView)
let indexPath = self.tableView.indexPathForRow(at: buttonPosition)
print("indexPath \(indexPath!)")
selectedIndexes[indexPath!] = !(selectedIndexes[indexPath!] ?? false)
tableView.reloadRows(at: [indexPath!], with: .automatic)
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: data.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
}
我需要帮助。如何通过点击图标+上的按钮添加新行
fun onPlusButtonClicked(){
sections.append(whatever you want)
items[2].append(["1", "2", "3", "4"]) // whatever you want to add here
tableview.reloadData() // you can call this on a background thread as well, if its not working
}
//如何与tableview一起使用的示例
var sections = Your array
var items = your array
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items[section].count
}
单击“添加”按钮时,不应重新加载整个表视图,因为这会增加处理时间。相反,你可以使用 单击按钮时插入新单元格的开始更新和结束更新 基本方法: 一,。单击Add,更新表视图的数据源
dataSource.append(NewRecord)
二,。插入新单元格:
tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: dataSource.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
查看您的代码:
您的数据源是创建和配置tableview的其他数据源。
但单击add button addButtonClicked函数时,您并没有更新其他数据源。请验证它,只是您的代码看起来不错。您是如何添加其余行的?程序基本相同…你犯了什么错误??我还可以看到您在使用行数时使用其他数组。但是您在AddButtonClickedal中没有使用相同的数组,那么您为什么需要这个呢??let buttonPosition=sender.convertCGPoint.zero,to:self.tableView let indexath=self.tableView.indexPathForRowat:buttonPosition错误为libc++abi.dylib:以NSException类型的未捕获异常终止我的数组是var data=[Guest 1]和var others:Results!。在那里,奥赫斯是王国的产物。阵列放置在哪里?您需要了解您的需求。理解我的答案,重新做一些工作。ex change section,如var sec:int sec=2,并在按钮中单击just increase section sec=sec+1,类似地,只需更新代码I获取错误libc++abi.dylib:以NSException类型的未捕获异常终止
func addButtonClicked(sender:UIButton) {
data.append("Guest 1")
.....
}