– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath没有被调用
所以它真的很奇怪…我有一个名为ListEventsViewController的ViewController
在头文件中:
@interface ListEventsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *eventsTable; @property (strong, nonatomic) NSArray *events; @end
在实现中,我打电话下面的几乎强制function:
- (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的图片。
您在截图中说明了您已将表链接到其IBOutlet,但尚未将表视图的委托或数据源链接到您的控制器,因此您的表委托方法(例如cellForRowAtIndex
将不会被调用。 链接这些应该可以解决你的问题。
我有这个&这一定是你的解决scheme
- (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; }
您可以在编码中使用以下几行:
tableview.delegate = self; tableview.datasource = self;
对我有什么帮助的是在表格中注释数字