我在情节提要中创建了一个表视图,并试图填充ViewController中的行。当我调用[self.tableView]时,它不会刷新内容(它不会调用cellForRowAtIndexPath)。
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
我究竟做错了什么?
最佳答案
我认为问题在于您有一个名为userSets的属性,还有一个名为userSets的ivar(我假设您有一个属性,即使您未显示它,因为使用self.userSets会给您带来错误)。在viewDidLoad中,您实例化了该属性,但是在fetchSets中,您尝试使用[userSets addObject:UserSet]
填充ivar,但从未初始化该数组。您应该摆脱ivar,并始终将您的属性称为self.userSets。
关于ios - UITableView不会在reloadData上重新加载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29652932/