收录日期:2019/06/20 13:36:39 时间:2010-06-22 20:55:05 标签:iphone,uitableview,uiwebview,multithreading,locking

I'm trying to add a UIWebView as a subview of a UITableViewCell in order to display some data formatted with MIMEType 'application/xhtml+xml'. Even just initializing the UIWebView as described in the following code leads the application to crash!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
NSLog(@"cellForRowAtIndexPath: '%d' in section '%d'", indexPath.section, indexPath.row);

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

UIWebView * webView = [[UIWebView alloc]initWithFrame:cell.frame];

return cell;

}

I'm getting this message:

bool _WebTryThreadLock(bool), 0x10a7f50: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...<

I can not figure out where I'm doing wrong!

Thanks for help

Gerald

The error message tells you what's going on: You are probably not on the main thread? You should only access UIKit from the main thread.

Additionally, cell.frame is most likely not what you want here. Try to set a fixed size with CGRectMake(...). And don't forget to add it. :-)