【问题标题】:Iphone Application:-My Application CrashIphone 应用程序:-我的应用程序崩溃
【发布时间】:2012-12-07 20:01:30
【问题描述】:

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

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

【问题讨论】:

  • 你有没有发送任何东西(特别是 tableview 单元格?)
  • 是的,我正在显示用户的推文,可以在文本视图上从推文中获取

标签: iphone objective-c uikit custom-cell


【解决方案1】:

这是因为您正在后台线程上进行一些 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
});

【讨论】:

  • 哪种类型的 UI 更改我的所有代码都如上,在 tableView:didSelectRowAtIndexPath 方法中我使用了类似的东西。 tweetview.text=//从推特获取
  • 或者,如果您更喜欢基于块的方法dispatch_async(dispatch_get_main_queue(), ^{ });
  • @iKAMBAD:那你是怎么得到推文的?
  • 类似这样的东西 TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET]; [请求 performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (error != nil) { NSError *error = nil; self.dataSource = [NSJSONSerialization JSONObjectWithData:responseData 选项:NSJSONReadingMutableLeaves 错误:&error];
  • 我的应用程序崩溃了,并在两个地方显示访问权限不正确,其中一个是 tweetview=[[UITextView alloc]init];第二个是 cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
【解决方案2】:

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

这可以使用:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-06-20
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    • 2016-01-25
    • 1970-01-01
    • 2011-10-06
    • 2010-10-14
    相关资源
    最近更新 更多