ios - 如何在每次刷新时从数组中的 UITableView 添加 30 个数据

标签 ios objective-c iphone

我是一名新的 iOS 程序员,但我遇到了一个问题。

我有一个 UITableView每次用户向下滚动到底部时,我想从数组中添加 30 个项目

请问你能帮帮我吗?

#import "ViewController.h"
@interface ViewController ()
{
    UIRefreshControl* refreshControl;
     int counter;
    //NSMutableArray *new;
}

@end

@implementation ViewController
@synthesize tableViewForScreen;
@synthesize array;

- (void)viewDidLoad
{
    [super viewDidLoad];

 refreshControl = [[UIRefreshControl alloc]init];
    [tableViewForScreen addSubview:refreshControl];
    [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
    //new =[[NSMutableArray alloc]init];
    array = [[NSMutableArray alloc] initWithCapacity:570];

    counter =30;
    if(counter>[array count]){


        counter = [array count];
    }

}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [ tableViewForScreen flashScrollIndicators];
}

- (void)viewDidUnload
{
    [self setTableViewForScreen:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark - Table View
// set number of Sections in Table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

 return 1;

}

// set number of Rows in each Sections of Table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return counter;
}

// Set animated style for Table Cell Editing
-(void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableViewForScreen setEditing:editing animated:animated];
}

// Customize the appearance of Table view cells.
- (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 = [array objectAtIndex:indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;
    // NSLog(@"offset: %f", offset.y);
    // NSLog(@"content.height: %f", size.height);
    // NSLog(@"bounds.height: %f", bounds.size.height);
    // NSLog(@"inset.top: %f", inset.top);
    // NSLog(@"inset.bottom: %f", inset.bottom);
    // NSLog(@"pos: %f of %f", y, h);

    float reload_distance = 10;
    if(y > h + reload_distance) {
        NSLog(@"load more rows");
        if(counter+30<[array count]){
            counter += 30;
            [tableViewForScreen reloadData];
            [tableViewForScreen flashScrollIndicators];
        } else if([array count]>counter) {
            counter = [array count];
            [tableViewForScreen reloadData];
            [tableViewForScreen flashScrollIndicators];
        }
    }
}
- (void)dealloc {
    [tableViewForScreen release];
    [super dealloc];
}

@end

最佳答案

这是一个 UIViewController 实现的示例,其中包含一个简单的 UITableView,称为“myTableView”(我想它已经将数据源和委托(delegate)设置为 ViewController)
我这个解决方案,我想你只有一个部分,所以每次用户将 tableView 滚动到底部时,计数器都会增加 30,以便在表中加载另外 30 个项目(如果有)

#import "ViewController.h"
@interface ViewController ()
{
     int counter;
}

@end

@implementation ViewController
@synthesize tableViewForScreen;
@synthesize array;

- (void)viewDidLoad
{
    [super viewDidLoad];

    [tableViewForScreen addSubview:refreshControl];
    array = [[NSMutableArray alloc] initWithCapacity:570];
    for(int i=0; i<570; i++)
        [array addObject:[NSString stringWithFormat:@"test%d",i]];

    counter=30;
    if(counter>[array count]){
        counter = [array count];
    }

}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [tableViewForScreen flashScrollIndicators];
}

- (void)viewDidUnload
{
    [self setTableViewForScreen:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

#pragma mark - Table View
// set number of Sections in Table
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

// set number of Rows in each Sections of Table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return counter;
}

// Set animated style for Table Cell Editing
-(void) setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableViewForScreen setEditing:editing animated:animated];
}

// Customize the appearance of Table view cells.
- (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 = [array objectAtIndex:indexPath.row];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;
    // NSLog(@"offset: %f", offset.y);
    // NSLog(@"content.height: %f", size.height);
    // NSLog(@"bounds.height: %f", bounds.size.height);
    // NSLog(@"inset.top: %f", inset.top);
    // NSLog(@"inset.bottom: %f", inset.bottom);
    // NSLog(@"pos: %f of %f", y, h);

    float reload_distance = 10;
    if(y > h + reload_distance) {
        NSLog(@"load more rows");
        if(counter+30<[array count]){
            counter += 30;
            [tableViewForScreen reloadData];
            [tableViewForScreen flashScrollIndicators];
        } else if([array count]>counter) {
            counter = [array count];
            [tableViewForScreen reloadData];
            [tableViewForScreen flashScrollIndicators];
        }
    }
}
- (void)dealloc {
    [tableViewForScreen release];
    [super dealloc];
}

@end

免责声明:为了检查何时到达底部,我使用了 solution

关于ios - 如何在每次刷新时从数组中的 UITableView 添加 30 个数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38196888/

相关文章:

ios - Objective C iOS - objc_object::sidetable_retain/release 在我的循环中消耗 CPU 时间?

ios - 从 UIWebView 中删除控制中心中的信息和控件

jquery - iPhone 上的菜单滚动

objective-c - 在不在 header 中定义的情况下在实现中创建方法

iphone - 在tableView中显示数组中的数据

iphone - 使用我自己的服务器创建安全 ios 身份验证的正确原因是什么

ios - Crashlytics Mac 应用程序如何工作?

ios - collectionViewCell 内的嵌入式 collectionView 未固定到安全区域布局

ios - 如何检测用户何时从 Facebook 删除应用程序

objective-c - 弹出错误 - [UIPopoverController dealloc] 在弹出窗口仍然可见时到达