向 UITableViewCell 添加新行
在我的应用程序中,我最初将在 UITableView
上仅显示一行。我想在用户加载前一行数据时增加行数(这里是 uiimage)。现在我在 numberOfRowsInSection:
方法中返回值 1,因为我不知道如何以所需的方式实现它。请帮忙。
我的 cellForRowAtIndexPath: 方法是
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier ] autorelease];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in nib){
if ([currentObject isKindOfClass:[CustomCell class]]){
cell = (CustomCell *)currentObject;
cell.viewController = self;
break;
}
}
}
if (j<15){
cell.imageView.image = nil;
if (count!=0)
{
@try{
NSInteger row = [indexPath row];
cell.imageView.image = [array objectAtIndex:row];
}
@catch (NSException* ex) {
NSLog(@"doSomethingFancy failed: %@",ex);
}
}
}
cell.showsReorderControl = YES;
return cell;
[array release];
}
if 条件和计数只是为了检查可变数组“array”的正确功能。
In my application,I will be displaying only one row on the UITableView
initially. I want to increase the rows as user loads the previous row with data(an uiimage, here). As now i'm returning value 1, in numberOfRowsInSection:
method, since I don't know how to implement it in the required way. Please help.
My cellForRowAtIndexPath:
method is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCellIdentifier = @"CustomCellIdentifier";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifier ] autorelease];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
for (id currentObject in nib){
if ([currentObject isKindOfClass:[CustomCell class]]){
cell = (CustomCell *)currentObject;
cell.viewController = self;
break;
}
}
}
if (j<15){
cell.imageView.image = nil;
if (count!=0)
{
@try{
NSInteger row = [indexPath row];
cell.imageView.image = [array objectAtIndex:row];
}
@catch (NSException* ex) {
NSLog(@"doSomethingFancy failed: %@",ex);
}
}
}
cell.showsReorderControl = YES;
return cell;
[array release];
}
that if condition and count is nothing but just for checking the correct functioning of the mutable array, 'array'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该阅读此文档: http:// developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/Introduction/Introduction.html
还可以查看相关示例。
注意:在您的代码中,不会调用 [array release],因为您在它之前有一个 return 语句。
You should read this documentation: http://developer.apple.com/iphone/library/documentation/UserExperience/Conceptual/TableView_iPhone/Introduction/Introduction.html
Have a look to the related samples as well.
Note: in your code [array release] won't be called since you've got a return statement just before it.
当您到达某个
indexPath
处的cellForRowAtIndexPath
时,这意味着该行正在显示。如果到达最后一行,您可以在表中插入新行。为此,您只需增加tableView:numberOfRowsInSection
函数返回的计数。我猜您的
numberOfRowsInSection
函数将如下所示:所以基本上,在 cellForRowAtIndexPath 内,添加以下代码:
您还需要刷新表格。一种方法是仅在 UITableView 上调用 reloadData,但首选方法是在表上调用 beginUpdates/insertRowsAtIndexPaths/endUpdates,以便可以显示正在插入的行的精美动画。
When you reach the
cellForRowAtIndexPath
at a certainindexPath
, it means that row is being displayed. If you reach the last row you can insert a new row into the table. To do that, you just need to increase the count that will get returned by thetableView:numberOfRowsInSection
function.I'm guessing your
numberOfRowsInSection
function will look something like this:So basically, inside cellForRowAtIndexPath, add the following code:
You also need to refresh the table. One way is to just call reloadData on your UITableView, but the preferred way is to call beginUpdates/insertRowsAtIndexPaths/endUpdates on the table so that you can show a nice animation of the row being inserted.