2012-09-05 39 views
3

因此,它確實很奇怪......我有一個視圖控制器稱爲ListEventsViewController- (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath不會被調用

在頭文件:

@interface ListEventsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
@property (weak, nonatomic) IBOutlet UITableView *eventsTable; 
@property (strong, nonatomic) NSArray *events; 

@end 

在執行我打電話下面幾乎強制性職能:

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = nil; 
    cell = [eventsTable dequeueReusableCellWithIdentifier:@"eventCell"]; 

    if(!cell){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"eventCell"]; 
    } 

    NSString *textLabelTitle = [[events objectAtIndex:indexPath.row] objectForKey:@"name"]; 
    NSString *textLabelDetail = [[events objectAtIndex:indexPath.row] objectForKey:@"date"]; 
    //cell.imageView.image = someimage; 
    NSLog(@"Text Label Title: %@", textLabelTitle); 
    NSLog(@"Text Label Detail: %@", textLabelDetail); 
    cell.textLabel.text = textLabelTitle; 
    cell.detailTextLabel.text = textLabelDetail; 

    return cell; 
} 

的問題是功能 - (UITableViewCell的*)的tableView:(UITa bleView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {未被調用,因此單元格不被填充。也沒有分配單元的數量。什麼可能導致這個?

它看起來接線到我這裏h和IB的圖片。 enter image description here

+1

是你確定你已經用IB或編程方式連接了委託屬性? –

+0

IB。看起來與我有聯繫。添加一張照片,以便您可以檢查只有incase :)。 – jimbob

+1

正如@Rickay所說,你的IB圖片顯示你沒有連接到你的ListEventsViewController的事件數據源和委託。 – iGranDav

回答

6

您說明在你的屏幕截圖,您已經鏈接的表格其IBOutlet中,但是你有沒有鏈接表視圖的委託或數據源到控制器,因此你的表委託方法如cellForRowAtIndex不會被調用。鏈接這些應該可以解決你的問題。

1

您可以使用您的代碼下面幾行:

tableview.delegate = self; 
tableview.datasource = self; 
2

我有這個&這必須B您的解決方案

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

if (cell==nil) { 
     cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
     mysampleimage = [[UIImageView alloc]initWithFrame:CGRectMake(3,3, 40, 40)]; 
     mysampleimage.image=[UIImage imageNamed:@"a.png"]; 
     mysampleimage.tag=13; 
     [cell.contentView addSubview:mysampleimage]; 

     myButton = [[UIButton alloc]init]; 
     myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     myButton.frame = CGRectMake(165, 10, 65, 30.); 
     [myButton setTitle:@"Show" forState:UIControlStateNormal]; 
     myButton.tag = 14; 
     [cell addSubview:myButton]; 

     lbl = [[UILabel alloc]initWithFrame:CGRectMake(50, 6, 200, 43)]; 
     lbl.tag=12; 
     [cell.contentView addSubview:lbl]; 
    } 
    else 
    { 
     lbl=(UILabel *)[cell.contentView viewWithTag:12]; 
     mysampleimage=(UIImageView *)[cell.contentView viewWithTag:13]; 
     myButton = (UIButton *)[cell.contentView viewWithTag:14]; 
    } 

    myButton.accessibilityLabel = lbl.text; 

    [myButton addTarget:self action:@selector(myShow:) forControlEvents:UIControlEventTouchUpInside]; 
    return cell; 


} 
1

什麼幫助我是註釋掉 numberofsectionsintableview

相關問題