在 TableView 中添加行 [英] Add row in a TableView

查看:54
本文介绍了在 TableView 中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个视图,第一个有我的 TableView,第二个有我的 TextField,我想在我的 TableView 中添加一行,并在我的 TextField 中添加文本.

So I have two views, the first have my TableView and the second have my TextField and I want to add a row in my TableView with the text in my TextField.

目前我可以用

[myTableView addObject:@"Test 1"];
[myTableView addObject:@"Test 2"];
[myTableView addObject:@"Test 3"];

感谢您的帮助!

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

NSString *cellValue = [myTableView objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

return cell;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [myTableView count];

}

推荐答案

我不确定你的问题到底是什么,但我认为如果你向我们展示你的表视图委托的 -tableView 会更清楚:numberOfRowsInSection:-tableView:cellForRowAtIndexPath: 方法.这些是您将更改表视图行为的关键点,这可能是您的答案开始的地方.

I'm not sure exactly what your question is here but I think it will be much clearer if you show us your table view delegate's -tableView:numberOfRowsInSection: and -tableView:cellForRowAtIndexPath: methods. These are the key points where you'll make changes to your table view's behavior, and this is where your answer is likely to start.

好的.这有点令人困惑——它看起来像 myTableView 是一个 NSArray?通常,具有类似名称的变量应该是指向表视图的指针.但是 UITableView 既没有 -addObject: 方法也没有 -count 方法.

Okay. This is a little confusing -- it looks like myTableView is an NSArray? Normally a variable with a name like that would be expected to be a pointer to a table view. But UITableView has neither an -addObject: method nor a -count method.

如果是这样,看起来你很好(虽然我真的认为你应该重命名该数组).您应该在 UITableView 上调用 -reload* 方法之一,让它知道数据已更改.最简单的是

If that is the case, it looks like you're good (though I really think you should rename that array). You should call one of the -reload* methods on your UITableView to let it know that the data has changed. The simplest is

[ptrToTableView reloadData];

但是只要多做一点工作,您就可以使用 -reloadSections:withRowAnimation: 获得更好的结果.

but with a little more work you can get fancier results with -reloadSections:withRowAnimation:.

如果这不能回答问题,那么我不确定问题是什么.:)

If this doesn't answer the question, then I'm not sure what the question is. :)

这篇关于在 TableView 中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆