问题:iphone读书应,加载文本会有些许延时,调用进度条,也要等文本加载完成后才显示出来,用线程来解决文本加载部分的方法和思路
1.调用显示UITableView,和进度条
[progress show:YES];
[UITableView initWithFrame:frame];
2.开新线和加载数据
UITableView
- (void) initWithFrame:frame {
[NSThread detachNewThreadSelector:@selector(getData:)toTarget:self withObject:filename];
}
3.加载数据
- (void) getData:(NSString *)filename {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
NSString *filePathA = [[NSBundle mainBundle] pathForResource:filename ofType:@"txt"];
NSString *s = [NSString stringWithContentsOfFile:filePathA encoding:NSUTF8StringEncoding error:nil];
[self performSelectorOnMainThread:@selector(setText:)
withObject:s
waitUntilDone:NO];
[loopPool drain];
}
4.设置内容
- (void) setText:(NSString *)s {
NSAutoreleasePool *loopPool = [[NSAutoreleasePool alloc] init];
tx.font = [[MDConfig shared] getFont:@"fontSize"];
tx.backgroundColor = [[MDConfig shared] getColor:@"fontBackColor"];
tx.textColor = [[MDConfig shared] getColor:@"fontForeColor"];
tx.text = s;
tx.userInteractionEnabled = YES;
//tx.pagingEnabled = YES;
tx.editable = NO;
CGSize totalTextSize = [s sizeWithFont:tx.font constrainedToSize:CGSizeMake(300.0f, CGFLOAT_MAX) lineBreakMode:UILineBreakModeWordWrap];
//tx.frame = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.bounds.size.height+20);
tx.contentSize = totalTextSize;
tx.contentOffset = CGPointMake(0.0f, 0.0f);
[[MDViewController shared] closeProgress];
//YH_RELEASE_SAFELY(tx);
[loopPool drain];
}
[[MDViewController shared] openProgress];
[[MDViewController shared] switchViews:@"bookView"];