向 UITableViewCell 添加新行

发布于 2024-08-13 18:34:24 字数 1549 浏览 3 评论 0原文

在我的应用程序中,我最初将在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名的好友 2024-08-20 18:34:24

您应该阅读此文档: 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.

梦里梦着梦中梦 2024-08-20 18:34:24

当您到达某个 indexPath 处的 cellForRowAtIndexPath 时,这意味着该行正在显示。如果到达最后一行,您可以在表中插入新行。为此,您只需增加 tableView:numberOfRowsInSection 函数返回的计数。

我猜您的 numberOfRowsInSection 函数将如下所示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section==0 ? [array count] : 0;
}

所以基本上,在 cellForRowAtIndexPath 内,添加以下代码:

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

     if([indexPath row]==[array count]-1) {
         [array addObject:(NEXT_IMAGE)];
     }
     return cell;
 }

您还需要刷新表格。一种方法是仅在 UITableView 上调用 reloadData,但首选方法是在表上调用 beginUpdates/insertRowsAtIndexPaths/endUpdates,以便可以显示正在插入的行的精美动画。

When you reach the cellForRowAtIndexPath at a certain indexPath, 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 the tableView:numberOfRowsInSection function.

I'm guessing your numberOfRowsInSection function will look something like this:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section==0 ? [array count] : 0;
}

So basically, inside cellForRowAtIndexPath, add the following code:

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

     if([indexPath row]==[array count]-1) {
         [array addObject:(NEXT_IMAGE)];
     }
     return cell;
 }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文