0

我制作了一个演示空应用程序并添加了一个带有视图的导航控制器

UINavigationController *navBar = [[UINavigationController alloc]initWithRootViewController:objFirstViewControllerViewController];
[self.window addSubview:navBar.view];

之后,我在第一个视图控制器上添加一个表格视图,如下所示。

UITableView* demotableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
demotableView.delegate = self;
demotableView.dataSource = self;
[self.view addSubview:demotableView];

以及像这样的行功能的表格视图和主单元格的委托功能

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  cell.textLabel.text = @"Hello this is sample text";
cell.textLabel.minimumFontSize = 12;
cell.textLabel.adjustsFontSizeToFitWidth = TRUE;
cell.textLabel.font = [UIFont fontWithName:@"Copperplate" size:18];
return cell;
}

但是,当我在我的表格上滚动或单击任何单元格以进入下一个视图时,它只会崩溃并分别在单击和滚动时出现这两个错误。

[__NSCFArray tableView:didSelectRowAtIndexPath:]
[__NSCFDictionary tableView:cellForRowAtIndexPath:]

我不明白它一直在正确使用先前的操作系统的这段代码出了什么问题

任何机构都可以帮忙吗?

这是选择行的代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Second *anotherViewController = [[Second alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];

}

没有一行是这个

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 15;
}
4

4 回答 4

1

实际上我滥用了 ARC 我为使应用程序成功运行所做的更改实际上由于内存泄漏而崩溃表的委托和数据源尝试在它发布的当前类中添加东西,并从这些消息实例中抛出消息,我被卡住了,因为我认为它正在发生,因为我采用了一个空的应用程序,但在添加了这些行之后在委托类下面我解决了问题。

我在.h文件的委托类中做了什么:

 FirstViewControllerViewController *objFirstViewControllerViewController;

@property (strong, nonatomic)  FirstViewControllerViewController *objFirstViewControllerViewController;

然后我的桌子开始表现正常,我遇到的所有问题。

于 2012-04-10T11:02:52.497 回答
0

用这个替换你的代码并尝试:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
  static NSString *CellIdentifier = @"Cell";

  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if(cell == nil)
  {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  }
  cell.textLabel.text = @"Hello this is sample text";

  cell.textLabel.minimumFontSize = 12;

  cell.textLabel.adjustsFontSizeToFitWidth = TRUE;

  cell.textLabel.font = [UIFont fontWithName:@"Copperplate" size:18];

  return cell;
}

希望这可以帮助。

于 2012-04-10T07:16:47.960 回答
0

由于您已设置demotableView.delegate = self; ,因此您必须实现tabelView: didSelectRowAtIndexPath:解决选择(单击)单元格时崩溃的功能。

要解决滚动崩溃,您必须实现

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

方法。

PS:在cellForRowAtIndexPath:中,除了cell.textLabel.text应该在里面的所有行

if(cell == nil){
}

请遵守适当的内存管理规则

于 2012-04-10T07:22:29.797 回答
0

请试试这个......

像这样添加您的表格视图..

 productList = [[UITableView alloc] initWithFrame:CGRectMake(0,102, 320, 267) style:UITableViewStylePlain];
 productList.delegate = self;
 productList.dataSource = self;
 productList.backgroundColor = [UIColor clearColor];
 [self.view addSubview:productList];

并添加这些方法....

  #pragma mark Table view data source

  // Customize the number of sections in the table view.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      return 1;
   }


 // Customize the number of rows in the table view.
   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return (your row count);
    }


 // Customize the appearance of table view cells.
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
     }

 // Configure the cell.

         cell.textLabel.text = @"Title";
         cell.detailTextLabel.text = formattedString;
         cell.detailTextLabel.textColor = [UIColor darkGrayColor];

         return cell;

      }

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
      Second *anotherViewController = [[Second alloc] initWithNibName:nil bundle:nil];
     [self.navigationController pushViewController:anotherViewController animated:YES];

   }
于 2012-04-10T07:38:04.987 回答