UITableView或UILabel加载大文本的慢的线程处理方法

问题: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 sharedcloseProgress];

//YH_RELEASE_SAFELY(tx);

[loopPool drain];

}


[[MDViewController sharedopenProgress];

[[MDViewController shared] switchViews:@"bookView"];

转载于:https://my.oschina.net/ahuaahua/blog/28618

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值