【问题标题】:tableview is crashing on clicking on cell or on scrolling in ios 5tableview 在单击单元格或在 ios 5 中滚动时崩溃
【发布时间】:2012-04-10 07:06:35
【问题描述】:

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

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

【问题讨论】:

  • 可以添加 didselectrow 和 numberofrows 方法的代码吗?
  • @RIP 我已添加代码,请检查
  • 您的错误表明 tableview:cellForRowAtIndexPath: 和 tableView:didSelectRowAtIndexPath: 正在发送到 NSArray 和/或 NSDictionary。您确定将实现 UITableViewDataSourceDelegate 协议的对象作为 demotableView.dataSource 传递吗?
  • 其实我只是用这个 demotableView.delegate = self; demotableView.dataSource = self;在 .h 中我声明我想使用表格视图的委托和数据源方法,除此之外,我不对单元格进行任何操作
  • @Ballu 似乎与某些变量的发布有关,请您仔细检查您在代码中编写的发布声明。

标签: iphone objective-c uitableview ios5


【解决方案1】:

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

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

 FirstViewControllerViewController *objFirstViewControllerViewController;

@property (strong, nonatomic)  FirstViewControllerViewController *objFirstViewControllerViewController;

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

【讨论】:

    【解决方案2】:

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

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

    希望这会有所帮助。

    【讨论】:

    • 在滚动和点击时仍然有同样的问题,我在替换代码后检查了对问题没有影响
    【解决方案3】:

    请试试这个....

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

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

    【讨论】:

    • 感谢 Ansul,但我也试过了,但还是有同样的问题...... :(
    • ok.. 只需做一件事,在 didselectrowatindexpath 方法中放置断点并检查...
    • 我设置了断点,但奇怪的是它从来没有进入表视图的委托和数据源函数并崩溃
    • 它似乎在 indexpath 函数的行的单元格中出现内存泄漏.. 我想是的
    • 告诉我你在同一个控制器或不同控制器中添加导航控制器和表格视图????因为如果您的导航工作正常,那么它也适用于表格视图,如果不是,那么首先检查您的导航控制器
    【解决方案4】:

    既然你已经设置了demotableView.delegate = self; 您必须实现tabelView: didSelectRowAtIndexPath: 函数,该函数解决了选择(单击)单元格时的崩溃问题。

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

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

    方法。

    P.S: 在cellForRowAtIndexPath: 中,除了cell.textLabel.text 之外的所有行都应该在里面

    if(cell == nil){
    }
    

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

    【讨论】:

    • 不,问题仍然存在,我已经完成了您要求做的事情,我为 noofrowsinsection 编写代码,并为确实选择行编写代码,并且我将这些行放入 if(单元格== nil){} 但它有问题..我正在从项目开始制作空应用程序,这可能是问题
    猜你喜欢
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 1970-01-01
    相关资源
    最近更新 更多