2013-09-20 70 views
1

我很困惑這一點。tableview:cellForRowAtIndexPath從來沒有叫

我想創建一個UITableView以編程方式分組,使用Xcode中的單個視圖模板。在互聯網上看一些例子,沒有什麼祕密,做這件事的方法非常簡單。我不知道如果我的實現是錯誤或正確的,因爲方法tableView:cellForRowAtIndexPath:從來沒有被調用,但其他的被調用,並返回一個> 0的整數。

有代碼,歡迎任何建議。 謝謝,

#import "ViewController.h" 
#import "SimpleCell.h" 

@interface ViewController() <UITableViewDelegate, UITableViewDataSource> 

@property (nonatomic, strong) UITableView *table; 
@property (nonatomic, strong) NSArray *sections; 
@property (nonatomic, strong) NSArray *section1; 
@property (nonatomic, strong) NSArray *section2; 
@property (nonatomic, strong) NSArray *section3; 

@end 

@implementation ViewController 

static NSString * const CellIdentfier = @"Cell"; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    [self.view addSubview:self.table]; 
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[table]|" 
                     options:0 
                     metrics:nil 
                     views:@{@"table": self.table}]]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.table reloadData]; 
    }); 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (UITableView *)table 
{ 
    if(!_table) 
    { 
     _table = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; 
     _table.delegate = self; 
     _table.dataSource = self; 
     _table.translatesAutoresizingMaskIntoConstraints = NO; 
     _table.rowHeight = 34.0f; 
     _table.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     _table.backgroundColor = [UIColor grayColor]; 
     _table.showsVerticalScrollIndicator = NO; 
     [_table registerClass:[SimpleCell class] forCellReuseIdentifier:CellIdentfier]; 
    } 

    return _table; 
} 

- (NSArray *)sections 
{ 
    if(!_sections) 
    { 
     _sections = @[@"Section1", @"Section2", @"Section3"]; 
    } 

    return _sections; 
} 

- (NSArray *)section1 
{ 
    if(!_section1) 
    { 
     _section1 = @[@"Player a", @"Player b", @"Player c"]; 
    } 

    return _section1; 
} 

- (NSArray *)section2 
{ 
    if(!_section2) 
    { 
     _section2 = @[@"Zone a", @"Zone b", @"Zone c"]; 
    } 

    return _section2; 
} 

- (NSArray *)section3 
{ 
    if(!_section3) 
    { 
     _section3 = @[@"Area a", @"Area b", @"Area c"]; 
    } 

    return _section3; 
} 


#pragma mark - UI Table View Delegate impl 


#pragma mark - UI Table View Datasource impl 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    DLog(); 
    SimpleCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentfier]; 

    if(!cell) 
     cell = [[SimpleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentfier]; 

    cell.label.text = [NSString stringWithFormat:@"Section: %i Row: %i", indexPath.section, indexPath.row]; 
    return cell; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger total = 0; 
    if(section == 0) total = self.section1.count; 
    if(section == 1) total = self.section2.count; 
    if(section == 2) total = self.section3.count; 

    DLog(@"Rows: %i", total); 

    return total; 
} 

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



@end 
+0

'numberOfRowsInSection:'中的NSLog'' self.section1'並查看它是否存在。 – Undo

+0

是的,這存在,輸出是:( 「玩家a」, 「玩家b」, 「玩家c」 ) –

回答

1

刪除此:

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[table]|" 
                    options:0 
                    metrics:nil 
                    views:@{@"table": self.table}]]; 

應該然後叫。

編輯:

變化這個問題,以及:

_table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped]; 

這似乎解決它,但我不知道爲什麼。我猜測它與表格框架必須大於0有關。

+0

我已經刪除,結果是一樣的。無論如何,我需要添加約束來在接口中顯示錶,因爲我使用CGRectZero創建了表框並將translatesAutoresizingMaskIntoConstraints設置爲「NO」。 –

+0

@JanCássio我已經更新了我的答案,看看是否解決了它。 – random

+0

哼,這還不夠,我需要將translatesAutoresizingMaskIntoConstraints設置爲「YES」,並且單元顯示爲接口。但是我受到很多限制的錯誤。檢查這一個: –