如何在Swift中将新单元格插入UITableView

编程入门 行业动态 更新时间:2024-10-04 05:30:32
本文介绍了如何在Swift中将新单元格插入UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个项目,当我有两个 UITableView 和两个 UITextField s时,用户按下按钮,第一个 textField 中的数据应进入 tableView ,第二个进入第二个的tableView 。我的问题是我不知道如何在每次用户按下按钮时将数据放入 tableView ,我知道如何使用插入数据tableView:cellForRowAtIndexPath:但据我所知,这有效一次。那么每次用户点击按钮时,我可以用什么方法来更新 tableView ?

I'm working on a project where I have two UITableViews and two UITextFields, when the user presses the button the data in the first textField should go into the tableView and the second go into the second tableView. My problem is that I don't know how to put data in the tableView each time the user pushes the button, I know how to insert data with tableView:cellForRowAtIndexPath: but that works for one time as far as I know. So what method can I use to update the tableView each time the user hits the button?

推荐答案

使用 beginUpdates 和 endUpdates 在单击按钮时插入新单元格。

Use beginUpdates and endUpdates for insert new cell when button clicked..

首先在tableview数组中附加数据

First of all append data in your tableview array

Yourarray.append([labeltext])

然后升级你的桌子并插入新行

Then upate your table and insert new row

// Update Table Data tblname.beginUpdates() tblname.insertRowsAtIndexPaths([ NSIndexPath(forRow: Yourarray.count-1, inSection: 0) ], withRowAnimation: .Automatic) tblname.endUpdates()

这会插入单元格而不需要重新加载整个表但是如果你遇到任何问题,你也可以使用 tableview.reloadData()

This inserts cell and doesn't need to reload whole table but If you get any problem with this,you can also use tableview.reloadData()

swift 3.0

tableView.beginUpdates() tableView.insertRows(at: [IndexPath(row: yourArray.count-1, section: 0)], with: .automatic) tableView.endUpdates()

Objective-c

[self.tblname beginUpdates]; NSArray *arr = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:Yourarray.count-1 inSection:0]]; [self.tblname insertRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationAutomatic]; [self.tblname endUpdates];

更多推荐

如何在Swift中将新单元格插入UITableView

本文发布于:2023-11-26 10:00:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633643.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   单元格   如何在   UITableView   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!