简体   繁体   English

UITableView不会在reloadData上重新加载

[英]UITableView does not reload on reloadData

I created a Table View in my Storyboard and am trying to populate the rows in my ViewController. 我在情节提要中创建了一个表视图,并试图填充ViewController中的行。 When I call [self.tableView], it doesn't refresh the content (it doesn't call cellForRowAtIndexPath). 当我调用[self.tableView]时,它不会刷新内容(它不会调用cellForRowAtIndexPath)。

SetListViewController.h: SetListViewController.h:

#import <UIKit/UIKit.h>

@interface SetListViewController : UITableViewController

@end

SetListViewController.m 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

What am I doing wrong? 我究竟做错了什么?

I think the problem is that you have a property called userSets, but also an ivar called userSets (I'm assuming you have a property even though you don't show it, since using self.userSets would give you an error otherwise). 我认为问题在于您有一个名为userSets的属性,还有一个名为userSets的ivar(我假设您有一个属性,即使您未显示它,因为使用self.userSets会给您带来错误)。 In viewDidLoad you instantiate the property, but in fetchSets you try to populate the ivar, with [userSets addObject:UserSet] -- but you never initialized that array. 在viewDidLoad中,您实例化了该属性,但是在fetchSets中,您尝试使用[userSets addObject:UserSet]填充ivar,但从未初始化该数组。 You should get rid of the ivar, and always refer to your property as self.userSets. 您应该摆脱ivar,并且始终将您的属性称为self.userSets。

Have you implemented the dataSouce methods of the table view: (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 您是否实现了表格视图的dataSouce方法: (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

and

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

Please make sure to implement above dataSouce methods of UITableView 请确保实现上述UITableView的dataSouce方法

first create the nib file with UIViewController. 首先使用UIViewController创建nib文件。 In your .h file set the following 在您的.h文件中设置以下内容

#import <UIKit/UIKit.h>

@interface SimpleTableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@end

In your .m file do the following 在您的.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;
}

In your .xib file do the following 在您的.xib文件中执行以下操作 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Try this out and reply what you found 试试看并回复您发现的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM