2016-07-05 60 views
1

我是一名新的iOS程序员,我遇到了一个问题。如何在阵列中每次刷新时在UITableView中添加30个数据

我有一个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 
+0

所以,你会喜欢在你的UiTableView的每个“reloadData”你会看到30个项目比以前重新加载? – ddb

+0

是@ddb我想在每次刷新时增加30个项目 –

+0

请检查我的回答:) – ddb

回答

2

这里是一个UIViewController的实现,它包含一个简单的UITableView,被称为“myTableView”的一个例子(我想它已经DataSource和委托集到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

+0

这不是一个好主意。它会将数据添加到表中,即使您只是想刷新/重新加载表。 – Mahesh

+0

@Mahesh,但是这是Swapnil Patel问的:)请检查第一个和第二个问题的评论 – ddb

+0

其不工作的结果就像test0,test1,test2,test0,test1,test2test0,test1,test2test0,test1,test2test0,test1,test2test0 ,test1,test2 ... n等 –

0

您可以使用下面的代码:

NSMutableArray *newInfos = //Your New data; 

NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:newInfos.count]; 
for(int i = 0; i < newInfos.count; i++) { 
    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:i inSection:0]; 
    [indexPaths addObject:indexPath]; 
} 

[self.tableView beginUpdates]; 
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationRight]; 
[self.tableView endUpdates]; 
0

你是什么意思你的意思是你得插图30个项目的tablview。所以只需更新数据源并使用该方法来更新UI

(void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation; 
+0

this (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation; –

+0

我想在每次重新加载时添加30个项目,例如1st reload 30,2ed reload -it将增加30个,所以count将会是60 –

+0

因此,只需在使用方法'reloadData'时更改dataSource。 –

相关问题