gpt4 book ai didi

ios - 如何使用刷新控件刷新 TableViewCell 数据

转载 作者:行者123 更新时间:2023-11-28 21:10:49 24 4
gpt4 key购买 nike

每当激活刷新控件时,我都试图将数据加载到 UITableViewCell 中。我的代码成功检索了数据,我可以使用 NSLog 验证这一点,但是当 Refresh Control 结束时 ViewCell 不会更新新数据。谁能指出我正确的方向?

下面是MyViewController.m文件:

#define JSON_URL @"https://website.com"
#import "MyViewController.h"
#import "Object.h"
#import "MyViewCell.h"
@interface MyViewController ()


@property (nonatomic,strong) NSMutableArray *objectHolderArray;
@end

@implementation MyViewController
- (void)viewDidLoad
{

NSURL *blogURL = [NSURL URLWithString:JSON_URL];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization
JSONObjectWithData:jsonData options:0 error:&error];
for (NSDictionary *bpDictionary in dataDictionary) {
Object *currenHotel = [[Object alloc]Station:[bpDictionary objectForKey:@"station"] Status:[bpDictionary objectForKey:@"status"]];
[self.objectHolderArray addObject:currenHotel];
}
[super viewDidLoad];

//to add the UIRefreshControl to UIView
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Please Wait..."]; //to give the attributedTitle
[refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];

}



- (IBAction)refresh:(UIRefreshControl *)sender {
[self viewDidLoad];
[sender endRefreshing];
}




#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{
return [self.objectHolderArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
MartaViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
Object *currentHotel = [self.objectHolderArray
objectAtIndex:indexPath.row];

cell.lblStation.text = currentHotel.station;
cell.lblStatus.text = currentHotel.status;
return cell;
}
-(NSMutableArray *)objectHolderArray{
if(!_objectHolderArray) _objectHolderArray = [[NSMutableArray alloc]init];
return _objectHolderArray;
}


@end

最佳答案

您需要重新加载数据。像这样实现 Refreshcontroll:

 - (void)viewDidLoad {
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self
action:@selector(update)
forControlEvents:UIControlEventValueChanged];

[self.startScreenView addSubview:refreshControl];
}

- (void) update {
[refreshControl beginRefreshing];
[yourTableView reloadData];
[refreshControl endRefreshing];
}

关于ios - 如何使用刷新控件刷新 TableViewCell 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458871/

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