Question

I have implemented a Twitter feed in a UITableViewController and I'm using a custom cell with a UITextView in it so it has link detection. The problem I'm having is that only the first cell shows up and if I start scrolling the app crashes with EXC_BAD_ACCESS on cell.tweetText.text = t[@"text"];. The feed shows up correctly if I don't use a custom cell, but then you can't tap on a link.

TweetCell.h

#import <UIKit/UIKit.h>

@interface TweetCell : UITableViewCell <UITextViewDelegate>

@property (weak, nonatomic) IBOutlet UITextView *tweetText;

@end

tableviewcontroller.h

#import <UIKit/UIKit.h>

@interface GRSTwitterTableViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *twitterFeed;

@end

tableviewcontroller.m

#import "GRSTwitterTableViewController.h"
#import "TweetCell.h"
#import "STTwitter.h"

@interface GRSTwitterTableViewController ()

@end

@implementation GRSTwitterTableViewController
@synthesize twitterFeed;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"";

    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];

    UIBarButtonItem *openTwitterButton = [[UIBarButtonItem alloc]initWithTitle:@"Open in Twitter" style:UIBarButtonItemStylePlain target:self action:@selector(openTwitter:)];
    self.navigationItem.rightBarButtonItem = openTwitterButton;

    STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"xz9ew8UZ6rz8TW3QBSDYg" consumerSecret:@"rm8grg0aIPCUnTpgC5H1NMt4uWYUVXKPqH8brIqD4o"];

    [twitter verifyCredentialsWithSuccessBlock:^(NSString *username)
    {
         [twitter getUserTimelineWithScreenName:@"onmyhonorband" count:50 successBlock:^(NSArray *statuses)
          {
              twitterFeed = [NSMutableArray arrayWithArray:statuses];
              [self.tableView reloadData];
          }
          errorBlock:^(NSError *error)
          {
              NSLog(@"%@", error.debugDescription);
          }];
    }
    errorBlock:^(NSError *error)
    {
        NSLog(@"%@", error.debugDescription);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)openTwitter:(id)sender
{
    NSURL *twitterURL = [NSURL URLWithString:@"twitter:///user?screen_name=onmyhonorband"];
    NSURL *safariURL = [NSURL URLWithString:@"https://twitter.com/onmyhonorband"];

    if ([[UIApplication sharedApplication]canOpenURL:twitterURL])
    {
        [[UIApplication sharedApplication]openURL:twitterURL];
    }
    else
    {
        [[UIApplication sharedApplication]openURL:safariURL];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [twitterFeed count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableView.backgroundColor = [UIColor darkGrayColor];

    TweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"TweetCell" bundle:nil] forCellReuseIdentifier:@"myCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
    }
    NSInteger idx = indexPath.row;
    NSDictionary *t = twitterFeed[idx];

    cell.tweetText.text = t[@"text"];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end

Here is a screenshot of what the tableview looks like when using the custom cell. enter image description here

Was it helpful?

Solution

replace the line code

TweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
 if (!cell)
 {
    [tableView registerNib:[UINib nibWithNibName:@"TweetCell" bundle:nil] forCellReuseIdentifier:@"myCell"];
    cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
 }

with the below code

TweetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (!cell)
{
    NSArray *nibs =[[NSBundle mainBundle] loadNibNamed:@"TweetCell" owner:self options:NULL];
    cell = [nibs firstObject];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top