2010-12-01 144 views
1

嘿,任何人,我在这里遇到一些与UITableView麻烦。UITableView崩溃

我设置我的UITableView使用XML动态创建行。在iPhone模拟器上一切正常,但是当我在设备上构建它时,当我将表格拖动到上或下时,应用程序崩溃。

我意识到的一点是,当某行离开屏幕时,应用程序崩溃。所以,当桌面在屏幕上仍然可见时,应用程序工作正常,但是当我将其拖出屏幕时,会崩溃。

这里去代码:

   #import "ComentariosViewController.h" 
       #import "TBXML.h" 


       @implementation ComentariosViewController 

       @synthesize listaComentarios, tabelaComentarios, nomesComentarios, rateComentarios; 


       - (void)viewDidLoad 
       { 


       listaComentarios = [[NSMutableArray alloc] init]; 
       nomesComentarios = [[NSMutableArray alloc] init]; 
       rateComentarios = [[NSMutableArray alloc] init]; 

       TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL     URLWithString:@"http://192.168.0.101/dev/mcomm/produto.xml"]] retain]; 
       TBXMLElement * rootXMLElement = tbxml.rootXMLElement; 

       TBXMLElement * comentarios = [TBXML childElementNamed:@"comentarios" parentElement:rootXMLElement]; 

       TBXMLElement * comentario = [TBXML childElementNamed:@"comentario" parentElement:comentarios]; 


       while (comentario) { 

        NSString * descText = [TBXML textForElement:comentario]; 

        NSString * nome = [TBXML valueOfAttributeNamed:@"nome" forElement:comentario]; 

        NSString * rate = [TBXML valueOfAttributeNamed:@"rate" forElement:comentario]; 

        [listaComentarios addObject:descText]; 
        [nomesComentarios addObject:nome]; 
        [rateComentarios addObject:rate]; 

        comentario = [TBXML nextSiblingNamed:@"comentario" searchFromElement:comentario]; 


       } 




       tabelaComentarios.separatorStyle = UITableViewCellSeparatorStyleNone; 
       tabelaComentarios.rowHeight = 105; 
       tabelaComentarios.backgroundColor = [UIColor clearColor]; 


       UIImageView *baloonTop = 
       [[[UIImageView alloc] 
        initWithFrame: 
        CGRectMake(165, 25, 43, 29)] 
        autorelease]; 

      baloonTop.image = [UIImage imageNamed:@"ComentsBaloon.png"]; 




      // Texto antes dos Comentarios 

      UIView *containerView = 
      [[[UIView alloc] 
       initWithFrame:CGRectMake(0, 0, 300, 70)] 
       autorelease]; 
      UILabel *headerLabel = 
      [[[UILabel alloc] 
       initWithFrame:CGRectMake(0, 20, 300, 40)] 
       autorelease]; 
      headerLabel.text = NSLocalizedString(@"Comentários", @""); 
      headerLabel.textColor = [UIColor grayColor]; 
      headerLabel.shadowColor = [UIColor whiteColor]; 
      headerLabel.shadowOffset = CGSizeMake(1, 0); 
      headerLabel.font = [UIFont boldSystemFontOfSize:26]; 
      headerLabel.backgroundColor = [UIColor clearColor]; 
      [containerView addSubview:headerLabel]; 
      [containerView addSubview:baloonTop]; 
      self.tabelaComentarios.tableHeaderView = containerView; 

      [tbxml release]; 

       } 

       // Numero de Secoes da Tabela (essecial) 

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

       // Numero de Linhas da Table (dinamico) 

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

       // Criacao e montagem da tabela 

       - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
       { 
      const NSInteger TOP_LABEL_TAG = 1001; 
      UILabel *topLabel; 
      UITextView *bottomLabel; 
      UIImageView *rateBase; 

      static NSString *CellIdentifier = @"Cell"; 
      UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) 
      { 
       // 
       // Create the cell. 
       // 
       cell = 
       [[[UITableViewCell alloc] 
       initWithFrame:CGRectMake(0, 0, 180, 200) 
       reuseIdentifier:CellIdentifier] 
       autorelease]; 


       // 
       // Create the label for the top row of text 
       // 
       topLabel = 
       [[[UILabel alloc] 
       initWithFrame: 
       CGRectMake(10, 5, 200, 20)] 
       autorelease]; 

       topLabel.font = [UIFont boldSystemFontOfSize:15]; 
       topLabel.textColor = [UIColor grayColor]; 

       [cell.contentView addSubview:topLabel]; 


       // Rates 

       rateBase = 
       [[[UIImageView alloc] 
       initWithFrame: 
       CGRectMake(215, 10, 67, 10)] 
       autorelease]; 

       NSString *rateValue = [rateComentarios objectAtIndex:indexPath.row]; 

       NSString *rateImage = [[NSString alloc] initWithFormat:@"Rate%@.png",rateValue]; 

       rateBase.image = [UIImage imageNamed:rateImage]; 

       [cell.contentView addSubview:rateBase]; 

       // Top Baloon 




       // 
       // Configure the properties for the text that are the same on every row 
       // 


       // 
       // Create the label for the top row of text 
       // 
       bottomLabel = [[[UITextView alloc] initWithFrame: CGRectMake(2, 28, 270, 58)] autorelease]; 
       [cell.contentView addSubview:bottomLabel]; 

       bottomLabel.editable = NO; 
       bottomLabel.scrollEnabled = NO; 

       // 
       // Configure the properties for the text that are the same on every row 
       // 
       NSString *cellValue =[listaComentarios objectAtIndex:indexPath.row]; 
       bottomLabel.text = cellValue; 

       bottomLabel.backgroundColor = [UIColor clearColor]; 
       bottomLabel.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0]; 
       //bottomLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0]; 
       bottomLabel.font = [UIFont systemFontOfSize:13]; 

       // 
       // Create a background image view. 
       // 
       cell.backgroundView = 
       [[[UIImageView alloc] init] autorelease]; 
       cell.selectedBackgroundView = 
       [[[UIImageView alloc] init] autorelease]; 
      } 

      else 
      { 
       topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG]; 
       //bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG]; 
      } 

      NSString *qlNome = [nomesComentarios objectAtIndex:indexPath.row]; 
      topLabel.text = qlNome; 

      NSString *cellValue =[listaComentarios objectAtIndex:indexPath.row]; 
      bottomLabel.text = cellValue; 








      // 
      // Set the background and selected background images for the text. 
      // Since we will round the corners at the top and bottom of sections, we 
      // need to conditionally choose the images based on the row index and the 
      // number of rows in the section. 
      // 
      UIImage *rowBackground; 
      //UIImage *selectionBackground; 
      rowBackground = [UIImage imageNamed:@"ComentariosBaloon.png"]; 
      //selectionBackground = [UIImage imageNamed:@"BkgComentarios.png"]; 
      ((UIImageView *)cell.backgroundView).image = rowBackground; 
      //((UIImageView *)cell.selectedBackgroundView).image = selectionBackground; 


      return cell; 
       } 


       - (void)dealloc 
       { 
      [tabelaComentarios release]; 
      [TBXML release]; 

      [super dealloc]; 
       } 


       @end 

感谢您的帮助!

+0

k,这是太多代码未格式化。你将不得不缩小问题的范围。控制台在崩溃时说什么?你改变了什么使它开始崩溃?等等。 – 2010-12-01 11:33:34

+0

选择您的代码并在编辑器中按'01010'按钮以正确格式化。并且不要将代码显然与您的问题无关,例如-numberOfSectionsInTableView:方法。让那些想要帮助你的人的生活变得更加容易 - 你可以通过查看你的代码来得到更快的答案 – Vladimir 2010-12-01 11:35:55

回答

0

如果单元格已成功从表视图中出列,则忽略将指针bottomLabel设置为任何特定值,然后隐式地将其解除引用。如果你真的不想当细胞离队然后初始化它指向零它指向什么:

UITextView *bottomLabel = nil; 

它总是安全的,在Objective-C消息为零。

你注释掉行:

//bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG]; 

不知道你的程序的更广阔的背景,我想这也可能是这个注释掉是一个错误。如果您希望在单元出列时设置bottomLabel的值,则可能需要恢复该值。