0

I'm displaying twitter feed in a UITableView. It loads properly. I've added a Bar button item (refresh) to refresh the table. However, clicking it doesn't reload the table. The onClick function is called but the code in it isn't executed.

//"TwitterViewController.h"
#import "ViewController.h"

@interface TwitterViewController : UITableViewController
- (IBAction)refresh:(id)sender;

@end

Here is a part of my "TwitterViewController.m"

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadTweets];
}


- (void)loadTweets
{

    STTwitterAPI *twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"consumer key"
                                                            consumerSecret:@"consumer secret"];
    [twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {

        [twitter getUserTimelineWithScreenName:@"username"
                                  successBlock:^(NSArray *statuses) {

                                      self.twitterFeed = [NSMutableArray arrayWithArray:statuses];

                                      [self.tableView reloadData];

                                  } errorBlock:^(NSError *error) {

                                      NSLog(@"%@", error.debugDescription);

                                  }];

    } errorBlock:^(NSError *error) {
        NSLog(@"%@", @"Error");

    }];
}



- (IBAction)refresh:(id)sender {
    [self.tableView reloadData];
}
1
  • 1
    reloadData on a UITableView doesn't call viewDidLoad again. You need to study the UITableViewDelegate and UITableViewDataSource protocols to better understand the reloadData method. Commented Feb 5, 2014 at 16:51

3 Answers 3

3

You need to re affect the value in twitterFeed array before to reload the tableview, otherwise data will be the same.

Simply call:

[self loadTweets];

instead of

[self.tableView reloadData];

so:

- (IBAction)refresh:(id)sender {
    [self loadTweets];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Alban, I did try that as well. However the function gets called but there is no change.
1

Try this it will be more elegant than reload data.

- (IBAction)refresh:(id)sender {
     [self loadTweets];
} 

In loadTweets:

    successBlock:^(NSArray *statuses) {
                                 [self.tableView beginUpdates];

                                  self.twitterFeed = [NSMutableArray arrayWithArray:statuses];

                                  [self.tableView endUpdates];
 }

6 Comments

For beginUpdates, the docs say: "You should not call reloadData within the group". loadTweets calls reloadData, so this isn't the way to go.
Yes you are correct sorry I had not gone through loadTweets. I will ammend my response.
Are you sure that the successBlock won't contain [self.tableView reloadData];? I tried your code but now the table doesn't load at all. The spinner keeps spinning.
Sorry I made a mistake. Should be beginUpdates and endUpdates. Don't need reloadData.
Hi Mikael, the app crashed with the following error - 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (20) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
|
1

Try this:

//"TwitterViewController.h"
#import "ViewController.h"

@interface TwitterViewController : UITableViewController
@property (strong, nonatomic) STTwitterAPI *twitter;
- (IBAction)refresh:(id)sender;

@end

TwitterViewController.m

- (void)loadTweets
{

    if (!self.twitter) {
      self.twitter = [STTwitterAPI twitterAPIAppOnlyWithConsumerKey:@"consumer key"
                                                            consumerSecret:@"consumer secret"];
    }
    __weak typeof(self) weakSelf = self;
    [self.twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {

    [weakSelf.twitter getUserTimelineWithScreenName:@"username"
                                  successBlock:^(NSArray *statuses) {

                                      weakSelf.twitterFeed = [NSMutableArray arrayWithArray:statuses];

                                      [weakSelf.tableView reloadData];

                                  } errorBlock:^(NSError *error) {

                                      NSLog(@"%@", error.debugDescription);

                                  }];

    } errorBlock:^(NSError *error) {
        NSLog(@"%@", @"Error");

    }];
}

2 Comments

Can you explain what you've done, and how that may help?
object 'STTwitterAPI' is removed before completion of the request

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.