如何在tableview的最后一行添加按钮?

时间:2011-08-23 11:47:17

标签: iphone objective-c cocoa-touch

我无法找到解决方法如何在UITableViewCell中添加此“加载更多”按钮。

喜欢此应用中的“更多结果...”选项

  

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

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

4 个答案:

答案 0 :(得分:2)

或许更简单的方法是采用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
}

答案 1 :(得分:0)

不是正常内容,而是将最后一个单元格设为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];
}

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

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

    if (section == 0){
        return dataForCells.count + 1;
    } else {
        return 0;
    }
}

答案 2 :(得分:0)

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

按钮加载更多。当用户点击buttonAction中的按钮时,取一个BOOL变量,然后使用[tableview reloadData];并在numberofrowsintableview方法中检查BOOL变量。如果是,则返回(Ex :) 10 + previousrows.Pass数据也

答案 3 :(得分:0)

我理解你的问题: -     你要做的是在表格视图下面添加一个活动指示器。

- (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];

}