2015-04-15 3 views
0

Я создал табличный вид в своей раскадровке и пытаюсь заполнить строки в моем ViewController. Когда я вызываю [self.tableView], он не обновляет содержимое (он не вызывает cellForRowAtIndexPath).UITableView не перезагружается при reloadData

SetListViewController.h:

#import <UIKit/UIKit.h> 

@interface SetListViewController : UITableViewController 

@end 

SetListViewController.m

#import "SetListViewController.h" 
#import "SetCell.h" 
#import "Set.h" 

@interface SetListViewController() <UITableViewDataSource, UITableViewDelegate> 
{ 
    NSMutableArray *userSets; // array containing all the user's set objects 
} 
@implementation SetListViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.userSets = [[NSMutableArray alloc] init]; 
    [self.tableView setDataSource:self]; 
    [self.tableView setDelegate:self]; 
    // load sets 
    [self fetchSets]; 
} 

- (void)fetchSets{ 
     for(NSDictionary *currentSet in set){ 
      Set *userSet = [[Set alloc] init]; 
      userSet.name = [currentSet objectForKey:@"name"]; 
      [userSets addObject:UserSet]; 
     } 
     [self.tableView reloadData]; 
    } 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return self.userSets.count; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return self.userSets.count; 
} 

- (SetCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"in cellForRowAtIndexPath"); 
    SetCell *cell = [tableView dequeueReusableCellWithIdentifier:@"setCell" forIndexPath:indexPath]; 
    NSDate *object = self.userSets[indexPath.row]; 
    cell.setTitleLabel.text = [object description]; 
    NSLog(@"setTitleLabel: %@", cell.setTitleLabel); 
    return cell; 
} 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     NSLog(@"deleting set"); 
     [self deleteSet:(NSString *)[self.userSets objectAtIndex:indexPath.row]]; 
     NSLog(@"set ID: %@", [self.userSets objectAtIndex:indexPath.row]); 
     [self.userSets removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 
@end 

Что я делаю неправильно?

+0

Вы установили источник данных? '[self.tableView setDatasource: self]' –

+0

Я добавил, что для viewDidLoad, но это не помогло решить проблему – scientiffic

+0

Делегаты настройки –

ответ

3

Я думаю, проблема в том, что у вас есть свойство, называемое userSets, но также и ivar, называемое userSets (я предполагаю, что у вас есть свойство, даже если вы его не показываете, так как использование self.userSets даст вам ошибку в противном случае). В viewDidLoad вы создаете экземпляр свойства, но в fetchSets вы пытаетесь заполнить ivar: [userSets addObject:UserSet] - но вы никогда не инициализировали этот массив. Вы должны избавиться от ivar и всегда ссылаться на свою собственность как на self.userSets.

+0

спасибо - это была ошибка! – scientiffic

1

Вы внедрили методы dataSouce представления таблицы: (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

и

(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

Пожалуйста, убедитесь, что для реализации описанных выше способов dataSouce из UITableView

+0

Да, у меня есть следующее: - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { return [self.userSets count]; } (и то же самое для numberOfRowsInSection) – scientiffic

0

сначала создать файл острия с UIViewController. В вашем файле .h установить следующие

#import <UIKit/UIKit.h> 

@interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

@end 

В файле .m сделайте следующее

@implementation SimpleTableViewController 
{ 
    NSArray *tableData; 
} 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Initialize table data 
    tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", nil]; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [tableData count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row]; 
    return cell; 
} 

В файле .xib сделайте следующее enter image description here

enter image description here

enter image description here

Попробуйте это и ответьте, что вы нашли

Смежные вопросы