【问题标题】:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath is not being called- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 没有被调用
【发布时间】:2012-09-05 23:15:44
【问题描述】:

所以它真的很奇怪......我有一个名为 ListEventsViewController 的 ViewController

在头文件中:

@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:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 没有被调用,因此单元格没有被填充。也没有分配单元格的数量。这可能是什么原因造成的?

我觉得这是一张 h 和 IB 的图片。

【问题讨论】:

  • 您确定您已在 IB 中或以编程方式连接了委托属性吗?
  • IB。看起来和我有关。添加了一张图片,这样您就可以检查以防万一:)。
  • 正如@Rickay 所说,您的 IB 图片显示您尚未连接到数据源并将您的 eventsTable 委托给您的 ListEventsViewController。
  • 哦,当然你也必须连接数据源!嗯....那行得通...我到了那里;)。干杯伙计们+!你们俩。

标签: ios uitableview


【解决方案1】:

您在屏幕截图中说明您已将表格链接到其 IBOutlet,但您尚未将表格视图的委托或数据源链接到您的控制器,因此您的表格委托方法(例如 cellForRowAtIndex)将不会被调用。链接这些应该可以解决您的问题。

【讨论】:

    【解决方案2】:

    我有这个,这必须是你的解决方案

    - (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;
    
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以在编码中使用以下几行:

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

      【讨论】:

        【解决方案4】:

        对我有帮助的是注释掉 可查看的部分数

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-03-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多