开发者

Trouble Reloading UITableView

开发者 https://www.devze.com 2023-02-16 21:11 出处:网络
I am trying to create an iphone application that reads a json feed and I am having some trouble updating a TableView.

I am trying to create an iphone application that reads a json feed and I am having some trouble updating a TableView.

This is my TableViewController header file:

@interface LiveNewsController : UITableViewController <UITableViewDelegate, UITableViewDataSource>{

NSMutableData *responseData;
NSMutableArray *articleArray;

UITableView *tblView;

}

-(void) refreshPressed: (id)sender;

@property (nonatomic, retain) IBOutlet UITableView *tblView;

@end

These are some parts of my TableViewController implementation file:

@implementation LiveNewsController

@synthesize tblView;

-(id) init {
    if(self = [super init])
    {
        self.title = @"Live News";
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"I am here %d",[articleArray count]);
    return [articleArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndex开发者_开发百科Path *)indexPath {

    static NSString *CellIdentifier = @"Cell";

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

    NSLog(@"cellForRowAtIndexPath called. Data size %d", [articleArray count]);


    NSUInteger row = [indexPath row];
    cell.textLabel.text = [articleArray objectAtIndex:row];

    return cell;
}

-(void) refreshPressed: (id)sender
{
    NSLog(@"Reload Pressed. Data size: %d", [articleArray count]);
    [tblView reloadData];
}

Here is a screenshot of my .xib file with the connections:

Trouble Reloading UITableView

So the main idea is the following:

  1. Fetch articleArray
  2. When refresh is pressed update view (the data is being fetched correctly but the snippet doesn't show it here)

numberOfRowsInSection and CellForRowAtIndexPath are only being called once when the view loads but nothing happens when I press the reload button.

I checked tblView and it is nil. I did some research and read that this is usually caused by some erroneous connections in the .xib file but I have triple checked that and I can't seem to pinpoint the problem.

Any help is appreciated,

Thanks!


You don't need to declare the UITableView tblView, the UITableViewController has already one (and therefore all subclasses too). Just try [self.tableView reloadData];.

0

精彩评论

暂无评论...
验证码 换一张
取 消