如何在UItableview中添加加载的footerview?

时间:2013-01-02 06:38:22

标签: iphone ios ipad

我创建了一个UITableView,我从Web服务中获取JSON数据。

Firat,我希望我先获得前5个对象并将其加载到tableView中。这种情况正在发生。

然后我希望每当用户滚动到页脚时..应该获取接下来的5个对象,它应该在tableView中显示..

我被困在这里..谁能让我知道如何继续下去?

我的tableview是否从不同类型的自定义类加载所有自定义单元格?

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if(section==[array count]-1)
    {
        UITableViewCell *cell = [self tableView:mtableview1 cellForRowAtIndexPath:myIP];
        frame =  cell.contentView.frame;
        footerView  = [[UIView alloc] init];
        footerView.backgroundColor=[UIColor clearColor];
        self.activityIndicator = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray] autorelease];
        self.activityIndicator.hidesWhenStopped=YES;

        self.activityIndicator.center = CGPointMake(65,50);
        [self.activityIndicator startAnimating];

        UILabel* loadingLabel = [[UILabel alloc]init];
        loadingLabel.font=[UIFont boldSystemFontOfSize:12.0f];
        loadingLabel.textAlignment = UITextAlignmentLeft;
        loadingLabel.textColor = [UIColor colorWithRed:87.0/255.0 green:108.0/255.0 blue:137.0/255.0 alpha:1.0];
        loadingLabel.numberOfLines = 0;
        loadingLabel.text=@"Loading.......";
        loadingLabel.frame=CGRectMake(95,35, 302,25);

        loadingLabel.backgroundColor =[UIColor clearColor];
        loadingLabel.adjustsFontSizeToFitWidth = NO;
        [footerView addSubview:self.activityIndicator];
        [footerView addSubview:loadingLabel];
        [self performSelector:@selector(loadending) withObject:nil afterDelay:1.0];
        [loadingLabel release];

        NSLog(@"%f",mtableview1.contentOffset.y);
        mtableview1.tableFooterView=footerView;

        return footerView;
    }
    return nil; 
}

我一直这样做,但它的位置一直不正确。这是我的大问题。

4 个答案:

答案 0 :(得分:4)

您可以使用以下概念。概念是当用户拉出表视图然后重新加载附加数据,如拉动刷新。

Code Sample :
//MARK: -UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView.contentOffset.y <= - 65.0f) {
        // fetch extra data & reload table view
    }
} 

答案 1 :(得分:0)

使用UIScrollViewDelegate方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    }

在scrollViewDidScroll方法中,您可以检查行索引以调用加载方法

NSIndexPath *indexPath = [[self.myTable indexPathsForVisibleRows]objectAtIndex:1]; //in middle visible of row
int visibleRowIndex = indexPath.row;

答案 2 :(得分:0)

您可以增加行数当您到达最后一行时,在表格视图中检查您是否有更多要显示的内容。如果您要显示MOre内容,则增加行数。

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

增加行数后,您可以滚动到想要显示的所需行...

答案 3 :(得分:0)

我已经实现了这一点,请看这里是我的代码

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{

    if(footerView == nil)
    {
        footerView  = [[UIView alloc] init];
        CGRect screenBounds = [[UIScreen mainScreen] bounds];

        if (ObjApp.checkDevice == 1)
        {
            UIImage *image = [[UIImage imageNamed:@"LoadMoreBtn.png"]
                              stretchableImageWithLeftCapWidth:8 topCapHeight:8];

            btnLoadMore = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnLoadMore setBackgroundImage:image forState:UIControlStateNormal];
            if(screenBounds.size.height == 568)
            {
                //    iPhone 5
                [btnLoadMore setFrame:CGRectMake(74, 13, 148, 34)];
            }
            else
            {
                //    iPohne 4 and Older iPhones
               [btnLoadMore setFrame:CGRectMake(74, 13, 148, 34)];
            }
        }
        else if (ObjApp.checkDevice == 2)
        {
            UIImage *image = [[UIImage imageNamed:@"LoadMoreBtn_iPad.png"]
                              stretchableImageWithLeftCapWidth:8 topCapHeight:8];

            btnLoadMore = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnLoadMore setBackgroundImage:image forState:UIControlStateNormal];
            [btnLoadMore setFrame:CGRectMake(220, 26, 296, 67)];
        }
        [btnLoadMore.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [btnLoadMore setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btnLoadMore addTarget:self action:@selector(btnLoadmore_clicked:)
         forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:btnLoadMore];
    }
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (ObjApp.checkDevice == 1)
    {
        return 47;
    }
    else
    {
        return 100;
    }
    return 0;
}

#pragma mark - Button Click Methods
-(void)btnLoadmore_clicked:(id)sender
{
    if(page > 0)
    {
        sendpage++;
        if(sendpage<= page)
        {
        [self loading];
        NSString *urlString = [NSString stringWithFormat:@"data={\"name\":\"%@\",\"lat\":\"%f\",\"page\":\"%d\",\"long\":\"%f\"}",txtSearchFld.text,ObjApp.current_Latitude,sendpage,ObjApp.current_Longitude];
        [WebAPIRequest WS_Search:self :urlString];

        CGFloat height = tblSearchResult.contentSize.height - tblSearchResult.bounds.size.height;
        [tblSearchResult setContentOffset:CGPointMake(0, height) animated:YES];
        }
    }
}