gpt4 book ai didi

ios - 通过代码错误使用refreshcontrol刷新tableView

转载 作者:行者123 更新时间:2023-11-29 03:39:26 26 4
gpt4 key购买 nike

我正在尝试使用refreshControl通过代码完成所有操作,当我拉动tableView并调用服务器时出现问题。它告诉我这个错误:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds for empty array'

当我运行这个简单的应用程序时,我会观察 TableView 中的数据。我不知道为什么会出现这个问题。

这是代码:

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * data;
@property (nonatomic, strong) UIRefreshControl *spinner ;
@end

@implementation YPProjectListViewController

@synthesize tableView;
@synthesize data;
@synthesize spinner;

- (void)viewDidLoad {
[super viewDidLoad];
spinner = [[UIRefreshControl alloc]initWithFrame:CGRectMake(130, 10, 40, 40)];
[self loadProjectsFromService];
[spinner addTarget:self action:@selector(loadProjectsFromService) forControlEvents:UIControlEventValueChanged];
[tableView addSubview:spinner];


}



-(void)loadProjectsFromService{
[spinner beginRefreshing];
self.data = [[NSMutableArray alloc] init];
[self.view addSubview:self.tableView];
__weak typeof(self) weakSelf = self;
successBlock = ^(NSMutableArray *newData) {
if ([newData count] > 0) {
[weakSelf refreshData:newData];
}

};
[spinner endRefreshing];
[ypNetManager getProjectListWithSuccessBlock:successBlock error:NULL];

}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Custom getter

- (UITableView *)tableView {
//custom init of the tableview
if (!tableView) {
// regular table view
tableView = [[UITableView alloc] initWithFrame:UIEdgeInsetsInsetRect(self.view.bounds, tableViewInsets) style:UITableViewStylePlain];
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
tableView.delegate = self;
tableView.dataSource = self;
tableView.backgroundColor = [UIColor clearColor];
return tableView;
}
return tableView;
}

#pragma mark - Private methods

- (void)refreshData:(NSMutableArray *)newData {
NSLog(@"data %@", newData);
self.data = newData;
[self.tableView reloadData];
}

最佳答案

我认为您从服务器获取数据没有任何问题。您可能做错的唯一一件事是在重新初始化 self.data 时不刷新 tableView。

当您下拉并释放 TableView 时,需要显示超出视口(viewport)的第 6 个 tableView 单元格,并且您的单元格需要数据中的第 6 个对象,但您的数据不再存在。

只需插入以下内容。

-(void)loadProjectsFromService{
[spinner beginRefreshing];
self.data = [[NSMutableArray alloc] init];

[self.tableView reloadData]; //Insert table reload here.

//... rest of your code ...
}

关于ios - 通过代码错误使用refreshcontrol刷新tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18664289/

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