【问题标题】:How to add a button to the last row of a tableview? [closed]如何在表格视图的最后一行添加按钮? [关闭]
【发布时间】:2011-08-23 11:47:16
【问题描述】:

我找不到解决方案如何在UITableViewCell 中添加这个“加载更多”按钮。

喜欢这个应用程序中的“更多结果...”选项

http://itunes.apple.com/in/app/zdnet/id425580940?mt=8

希望有人可以帮助我...

【问题讨论】:

  • 你到底想做什么。您是否希望在向下滚动表格时加载更多内容,直到没有出现新结果。
  • 我无法在 Tableview 中放置该按钮。就像我在表中有 10 行一样,那么如何将“loadmore”选项放在第 11 行。?你能给我一些链接或一些代码吗?

标签: iphone objective-c cocoa-touch


【解决方案1】:

也许更简单的方法是采用tableView:viewForFooterInSection:tableView:heightForFooterInSection 方法并使用“加载更多结果”按钮返回一个视图。

-(id)init
{
    self = [super init];
    if(self) {
        // create a footer view so that we can add it later when we need to
        loadMoreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
        UIButton* moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [[moreButton titleLabel] setText:@"Load More Results"];
        [moreButton addTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
        [loadMoreView addSubview:moreButton];            

        // other init code here
    }
    return self
}

-(UIView*)tableView:(UITableView*)tv viewForFooterInSection:(NSInteger)s
{
    return loadMoreView;
}
-(CGFloat)tableView:(UITableView*)tv heightForFooterInSection:(NSInteger)s
{
    return [loadMoreView frame].size.height; // or 40 since that's what we set it to
}

【讨论】:

  • 我认为,这是一种更清洁的方法。但是你还需要为 UIButton 指定框架; moreButton.frame = CGRectMake(0, 0, 160.0, 40.0);
【解决方案2】:

据我所知,您可以这样做。

放一个按钮 loadmore 。当用户单击 buttonAction 中的按钮时,获取一个 BOOL 变量,然后使用 [tableview reloadData];。在 numberofrowsintableview 方法中检查 BOOL 变量。如果是,则返回(例如:) 10+previousrows.将数据也传递给它

【讨论】:

    【解决方案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];
        }
        cell.textLabel.text=[self.array objectAtIndex:indexPath.row];
    
        if (indexPath.row ==[array count] -1 ) { //this method will get called when you will scroll to the bottom of your table view 
            [self sendRequestMethod];   
    
        }
        else {
            //NSLog(@"not nearest row");
            //[self remove];
        }
    
        return cell;
    }
    
    -(void)sendRequestMethod
    {
        [self.newTableView setUserInteractionEnabled:NO];
        [indicator startAnimating];//make sure you start your indicator now perform what you want to do IN my case I am sending a request to server
    
        NSString *str = [NSString stringWithFormat:@"http://www.google.com"];
        NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setRequestMethod:@"POST"];
        [request setDelegate:self];
        [request setDidFinishSelector:@selector(responseSuccedMethod:)];
        [request setDidFailSelector:@selector(responseFailedMethod:)];
        [networkQueue addOperation: request];
        [networkQueue go];
    
    
    }
    
    -(void)responseSuccedMethod:(ASIHTTPRequest *)req
    {   
        [self.newTableView setUserInteractionEnabled:YES];
        [indicator stopAnimating];  
        [newTableView reloadData];
    
    }
    

    【讨论】:

    • 感谢您的回复,但我是 iPhone 开发新手,所以我无法在 Tableview 中放置该按钮。就像我在表格中有 10 行一样,我该如何放置“loadmore”第 11 行的选项。?你能给我一些链接或一些代码吗?
    【解决方案4】:

    将您的最后一个单元格设置为 UIButton,而不是您的正常内容。当用户单击它时,使用新信息更新您的数据源(不要忘记更新每个部分中的行数),然后在 tableview 上调用 reloadData。始终将按钮保留在最底部的单元格中。

    如何更新数据源完全取决于您的实施。

    要向 UITableViewCell 添加按钮,您可以在 cellForRowAtIndexPath 中执行此操作:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        if (indexPath.row == dataForCells.count + 1){ // Assume dataForCells is an NSArray holding your data.  Initially it should hold 10 objects, then after clicking this button it will be 20, 30, etc. as you load data in load10MorePressed.  However, this depends on your implementation.  You could be storing your data in some other way.
            static NSString *CellIdentifier = @"LoadMoreCell";
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            }
    
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectInset(cell.frame, 5, 5);
            [button setTitle:@"Load 10 More" forState:UIControlStateNormal];
            [button addTarget:self action:@selector(load10MorePressed:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:button];
        } else {
            static NSString *CellIdentifier = @"NormalCell";
    
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
            }
    
            // Setup your normal data cells here
        }
    }
    

    但你需要确保你有一个名为 load10MorePressed 的方法:

    -(IBAction)load10MorePressed:(id)sender{
        NSMutableArray *temp = [NSMutableArray arrayWithArray:dataForCells];
        for (int x=0; x<10; x++){
            [temp addObject:[Create new objects here.  Depends on your implementation, probably downloading from someplace...]];
        }
        self.dataForCells = temp;
        [self.tableView reloadData];
    }
    

    最后,不要忘记返回表格视图中的单元格数量:

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        if (section == 0){
            return dataForCells.count + 1;
        } else {
            return 0;
        }
    }
    

    【讨论】:

    • 感谢您的回复,但我是 iPhone 开发新手,所以我无法在 Tableview 中放置该按钮。就像我在表格中有 10 行一样,我该如何放置“loadmore”第 11 行的选项。?你能给我一些链接或一些代码吗?
    • 我已经更新了我的回复,以展示如何创建单元格并加载更多数据。
    • 我以这种方式从 XML 获取数据(我在每个页面中获取 10 个新闻,有 10 个页面,我通过更改“%@”的值来获取这些新闻(占位符)在 api 中,所以我再次无法做到这一点我真的很感谢你的回应我在这个解决方案上工作你给我但我再次无法理解这个问题(我有一个类似的 API:-@987654321 @ 我正在将此占位符“%@”的值从 1 更改为 10,每页有 10 个新闻,每页有 10 个新闻,所以我的问题是我能够解析以显示在表格中,但是我如何在 10 个新闻之后设置加载更多按钮将显示) ...
    猜你喜欢
    • 2013-04-24
    • 2015-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    • 2017-03-06
    • 1970-01-01
    • 2018-07-29
    相关资源
    最近更新 更多