ahy*_*dev 5 iphone objective-c uitableview ios
#import "AssignmentsViewController.h"
#import "Assignment.h"
@interface AssignmentsViewController ()
@property NSMutableArray *assignments;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation AssignmentsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(@"viewDidLoad");
[super viewDidLoad];
// Do any additional setup after loading the view.
self.assignments = [[MySingleton sharedMySingleton] assignments];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)viewWillAppear:(BOOL)animated {
NSLog(@"viewWillAppear");
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"numberOfSectionsInTableView");
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"numberOfRowsInSection");
return [self.assignments count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"cellForRowAtIndexPath");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AssignmentCell"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AssignmentCell"];
}
cell.textLabel.text = [[self.assignments objectAtIndex:indexPath.row] title];
return cell;
}
@end
Run Code Online (Sandbox Code Playgroud)
加载AssignmentsViewController并且至少有一个家庭作业在assignments数组中时,控制台输出:
2013-11-06 18:55:09.396 Homework Planner[4756:70b] viewDidLoad
2013-11-06 18:55:09.403 Homework Planner[4756:70b] numberOfSectionsInTableView
2013-11-06 18:55:09.405 Homework Planner[4756:70b] numberOfRowsInSection
2013-11-06 18:55:09.408 Homework Planner[4756:70b] viewWillAppear
2013-11-06 18:55:09.409 Homework Planner[4756:70b] numberOfSectionsInTableView
2013-11-06 18:55:09.409 Homework Planner[4756:70b] numberOfRowsInSection
Run Code Online (Sandbox Code Playgroud)
我正在创建一个显示用户家庭作业的应用程序.由于某种原因,即使我确定赋值数组中有一个家庭作业分配对象,也从不调用cellForRowAtIndexPath.通过模态视图将作业分配添加到数组中,并且我已经验证它们确实是由该过程添加的,并且在用户点击"取消"之后视图返回到由该类定义的视图.当发生这种情况时,另外两个与UITableView相关的方法运行,而不是与cellForRowAtIndexPath相关.请帮我解决这个问题.
让我再提出一个罪魁祸首:"内存管理问题".
如果TableView的DataSource对象没有保留(保留计数为零)而TableView正在从中提取数据,那么,令人惊讶的是,尽管numberOfSections和numberOfRows刚刚被调用,但不会调用tableView:cellForRowAtIndexPath:
检查是否[self.assignments count]返回0(检查是否self.assignments是nil.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"[self.assignments count] = %d", [self.assignments count]);
}
Run Code Online (Sandbox Code Playgroud)
否则,我现在能想到的另一个原因是,当委托方法返回的高度为0时(如果你正在实现它,否则默认实现总是给出非零高度,所以在这种情况下没有问题.)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = 0;
// Some calculation to determine height of row/cell.
return height;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8247 次 |
| 最近记录: |