我试图做的是解析 XML 数据并将其显示在我的 UITableView 中,但问题是它在 2-3 秒后显示,我试图在数据加载时包含一个 UIActivityIndicator 并且我也试图包含一个 gcd,但是我对这些东西很陌生,所以我真的很困惑该做什么或我可以在哪里放置 gcd 代码。
那是我的 .m 文件。
@implementation ViewController
@synthesize listTableView;
dispatch_queue_t myQueue;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[xmlParser listPopulated]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"CustomCell";
dataFileHolder *currentData = [[xmlParser listPopulated] objectAtIndex:indexPath.row];
CustomCellXMLClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[CustomCellXMLClass alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCellXMLSample" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSString *nameLabel = [currentData nameOfCat];
NSString *dataToCacheLabel = [myCache objectForKey:nameLabel];
if(nameLabel != nil){
dataToCacheLabel = [NSString stringWithString:nameLabel];
if (dataToCacheLabel != nil) {
[myCache setObject:dataToCacheLabel forKey:nameLabel];
[cell.nameLabel setText:dataToCacheLabel];
}
}
NSString *detailLabel = [currentData descriptionOfCat];
NSString *stringToCache = [myCache objectForKey:detailLabel];
if (detailLabel != nil) {
stringToCache = [NSString stringWithString:detailLabel];
if (stringToCache != nil) {
[myCache setObject:stringToCache forKey:detailLabel];
[cell.detailLabel setText:stringToCache];
}
}
NSString *imageURL = [currentData imageLink];
NSData *dataToCache;
if (imageURL != nil) {
dataToCache = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
if (dataToCache != nil) {
[myCache setObject:dataToCache forKey:imageURL];
[cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
}
else {
NSURL *imageURL = [NSURL URLWithString:@"http://i178.photobucket.com/albums/w255/ace003_album/190579604m.jpg"];
dataToCache = [NSData dataWithContentsOfURL:imageURL];
[myCache setObject:dataToCache forKey:imageURL];
[cell.imageShow setImage:[UIImage imageWithData:dataToCache]];
}
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 78;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString *title = @"Sample View";
return title;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.activityIndicator startAnimating];
self.activityIndicator.hidesWhenStopped = YES;
dispatch_queue_t myQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(myQueue, ^ {
xmlParser = [[XMLParser alloc]loadXMLByURL:@"http://www.irabwah.com/mobile/core.php?cat=0"];
[self.activityIndicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
});
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end