Iphone应用程序: - 我的应用程序崩溃

时间:2012-12-20 10:14:45

标签: iphone objective-c uikit custom-cell

我制作了一个这样的自定义单元格类。

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {

    /*imgview=[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 40, 20)];
    imgview.backgroundColor = [UIColor clearColor];
    imgview.opaque = NO;*/

    Name = [[UILabel alloc]initWithFrame:CGRectMake(75, 10, 130, 30)];
    Name.backgroundColor = [UIColor clearColor];
    Name.textColor=[UIColor blueColor];
    Name.font=[UIFont fontWithName:@"Arial" size:16.0];
    Name.textAlignment=NSTextAlignmentLeft;

    CGSize textSize = [[Name text] sizeWithFont:[Name font]];
    CGFloat strikeWidth = textSize.width;
    ScreenName =[[UILabel alloc]initWithFrame:CGRectMake(strikeWidth+75.0, 25  , 150, 40)];
    ScreenName.backgroundColor = [UIColor clearColor];
    ScreenName.textColor=[UIColor redColor];
    ScreenName.font=[UIFont fontWithName:@"Arial" size:16.0];
    ScreenName.textAlignment=NSTextAlignmentLeft;

    tweetview=[[UITextView alloc]initWithFrame:CGRectMake(75, 50, 200, 70)];
    tweetview.backgroundColor = [UIColor clearColor];
    tweetview.textColor=[UIColor redColor];
    tweetview.font=[UIFont fontWithName:@"Arial" size:16.0];        
    tweetview.textAlignment=NSTextAlignmentLeft;
    tweetview.editable=NO;
    tweetview.dataDetectorTypes=UIDataDetectorTypeLink;

    Minutes=[[UILabel alloc]initWithFrame:CGRectMake(270, 20, 150, 50)];
    Minutes.backgroundColor = [UIColor clearColor];
    Minutes.textColor=[UIColor blueColor];
    Minutes.font=[UIFont fontWithName:@"Arial" size:14.0];
    Minutes.textAlignment=NSTextAlignmentLeft;

    //  [self.contentView addSubview:imgview];
    [self.contentView addSubview:Name];
    [self.contentView addSubview:ScreenName];
    [self.contentView addSubview:Minutes];

    //  [self.contentView addSubview:LastTweet];
    [self.contentView addSubview:tweetview];

}

return self;
}

并在nextviewcontroller.m实例方法tableView:didSelectRowAtIndexPath:中使用它,如下所示:

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

}

我的应用程序每次都会崩溃

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...

2 个答案:

答案 0 :(得分:1)

如@ Rajneesh071所述,必须在主线程上执行UI更改。

这可以使用:

完成
[self performSelectorOnMainThread:@selector(doYourUIChanges:) withObject:nil waitUntilDone:YES];

或者,如果您想在主线程上执行一段内联代码,请使用:

dispatch_async(dispatch_get_main_queue(), ^{
    /* YOUR UI STUFF */
});

答案 1 :(得分:0)

这是因为您在后台线程上进行了一些UI更改。 在ios中,您无法在后台线程上执行任何UI更改,这会使您的应用程序崩溃。

所以你必须在主线程上进行UI更改

[self performSelectorOnMainThread:@selector(doYourUIChanges:) withObject:nil waitUntilDone:YES];  

dispatch_async(dispatch_get_main_queue(), ^{ 
//Your Task
});

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // background code
});