简体   繁体   English

如何在同一视图的iPhone中的UITableView中重新加载数据

[英]How to reload the data in UITableView in iPhone in the same view

I am developing an iPhone application, in a view, I have placed UITextView and UITableView in the same ViewController . 我正在开发iPhone应用程序,在一个视图中,我已将UITextViewUITableView放在同一ViewController What i need to do is, after entering the data in the textview, the user will click on a button placed below. 我需要做的是,在textview中输入数据后,用户将单击下面的按钮。 After clicking on the button, the data entered in the textview should be loaded in the tableview. 单击按钮后,应将文本视图中输入的数据加载到表格视图中。 But it is not working so. 但这行不通。

Here, what i have done is, I have written the below code inside the button action, 在这里,我所做的是,我在按钮动作中编写了以下代码,

[self.tableView reloadData];

Any suggestions much appreciated 任何建议,不胜感激

Make sure you are updating the NSMutableArray with the new value, that you are using to fill data in your UITableView . 确保使用新值更新NSMutableArray ,以用于在UITableView填充数据。

On the click of the button, 点击按钮,

[arrValues addObject:txtBox.text];
[self.tableView reloadData];

This method handle your rowCount of TableView 此方法处理您的TableView的rowCount

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [arrayData count];
}

And this method will make your cell and showing cell data. 并且此方法将使您的单元格显示单元格数据。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

So you will have to add textField.text in that array which you're using for numberOfRowsInSection: and cellForRowAtIndexPath: 因此,您将必须在要用于numberOfRowsInSection:cellForRowAtIndexPath: array添加textField.text cellForRowAtIndexPath:

-(void)buttonClick:(id)sender
 {
     [arrayData addObject:textField.text];
     [self.tableView reloadData];
 }

And make sure textField.text should not be empty other wise data will not be visible. 并确保textField.text不应为空,否则将不可见数据。 After calling [self.tableView reloadData] method all tableView delegates method will be call again and arrayData will increase the numberOfRows for your tableView . 调用[self.tableView reloadData]方法后,将再次调用所有tableView委托方法,并且arrayData将增加tableViewnumberOfRows

EDIT: As you're saying in comment that you're navigating the view so each time your viewDidLoad: will be call and your array will have the same object which your initialize in viewDidLoad: if you want that your array remain same objects then dont initialize it static, use plist or Database and save your array data in Database in viewDidDisappear: method and refresh your array in viewWillAppear: method before adding your tableView . 编辑:正如您在评论中说的那样,您正在浏览视图,因此每次调用viewDidLoad:时,您的数组将具有与在viewDidLoad:初始化的对象相同的对象viewDidLoad:如果您希望array保持相同的objects则不要将其静态初始化,使用plistDatabase并将array数据保存在viewDidDisappear:方法中的Database中,并在添加tableView之前在viewWillAppear:方法中刷新array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM