0

I make one custom cell class like this.

- (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;
}

and use it in nextviewcontroller.m instance method tableView:didSelectRowAtIndexPath: like this:

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];

}

my application is crashing every time with message

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...
Alessandro Vendruscolo
  • 14,493
  • 4
  • 32
  • 41

2 Answers2

1

As mentioned by @Rajneesh071, UI changes must be performed on the main thread.

This can be done using:

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

Or if you want a section of in-line code to be performed on the main thread, use this:

dispatch_async(dispatch_get_main_queue(), ^{
    /* YOUR UI STUFF */
});
colincameron
  • 2,696
  • 4
  • 23
  • 46
0

This is because you are doing some UI changes on background thread. In ios you can not perform any UI changes on background thread , this will crash your app.

So you have to do your UI changes on main thread

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

Or

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

or

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // background code
});
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
  • which type of UI changes all my code are as above and in tableView:didSelectRowAtIndexPath method i used something like this. tweetview.text=//fetching from twitter –  Dec 20 '12 at 10:37
  • Or, if you prefer a block-based approach `dispatch_async(dispatch_get_main_queue(), ^{ });` – jnpdx Dec 20 '12 at 10:37
  • 1
    @iKAMBAD: then how did you get the tweets ? – Midhun MP Dec 20 '12 at 10:44
  • something like this TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET]; [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { if (error != nil) { NSError *error = nil; self.dataSource = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error]; –  Dec 20 '12 at 10:47
  • my application is crash and show me bad access at two place one is here tweetview=[[UITextView alloc]init]; and second one is here cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease]; –  Dec 20 '12 at 10:52
  • so just do this both work on mainThread ... just make a function(doYourUIChanges) and in this function put your code ..then call your function as i mention in my answer.. – Rajneesh071 Dec 21 '12 at 07:46