gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:08:18 27 4
gpt4 key购买 nike

我是一名新的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com