2011-04-29 66 views
1

tableview問題:我使用3 uilable顯示產品名稱,一些說明和圖像。所有的數據都顯示出來,但是當滾動表格的時候,標籤會被其他文字填滿實際的文字......我們該怎麼處理? 代碼:問題與表視圖分組表視圖

- (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] autorelease]; 

    } 


UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 41, 41)]; 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 

    NSDictionary *aDict1 = [[NSDictionary alloc]init]; 
    aDict1 = [tableData objectAtIndex:indexPath.row]; 

    NSString *prdStus = [aDict1 objectForKey:@"ProductStatus"]; 
    NSLog(@"product status is %@ ",prdStus); 
    if ([prdStus isEqualToString:@"Orange"]) { 
     [imageview setImage:[UIImage imageNamed:@"Yellow.png"]]; 
    } 
    if ([prdStus isEqualToString:@"Green"]) { 
     [imageview setImage:[UIImage imageNamed:@"Green.png"]]; 
    } 
    if ([prdStus isEqualToString:@"Red"]) { 
     [imageview setImage:[UIImage imageNamed:@"Red.png"]]; 
    } 

    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(46, 0, 220, 12) ]; 
    label2.font = [UIFont systemFontOfSize:12]; 
    label2.text = @""; 
    if (indexPath.row <[tableData count]) { 
     label2.text = [aDict1 objectForKey:@"ProductName"]; 
    } 

    [cell addSubview:label2]; 


    [cell addSubview:imageview]; 
    label2.backgroundColor =[UIColor clearColor]; 
    UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(46, 13, 220, 30) ]; 
    label3.font = [UIFont systemFontOfSize:10]; 
    label3.text = @""; 
    label3.text = [aDict1 objectForKey:@"ProductDescription"]; 

    [cell addSubview:label3]; 


    return cell; 

} 

請告訴我如何避免這個..

分組表視圖。

這裏也是我面臨同樣的問題。 我使用4節 第一和第三部分,每1行, 第二秒3行, 第四秒5

時配置在第一部分的標籤顯示在第四和第三部分數據中的文本也顯示在第四部分。 代碼

- (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] autorelease]; 

    } 

    cell.userInteractionEnabled =NO; 

if (indexPath.section == 0) 

    { 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 2, 294, 40) ]; 
     label.backgroundColor = [UIColor clearColor]; 
     NSDictionary *aDict1 = [[NSDictionary alloc]init]; 
     aDict1 = [detailsArray objectAtIndex:0]; 

     [email protected]""; 
     label.text =[aDict1 objectForKey:@"ProductName"]; 
     label.numberOfLines = 2; 
     label.lineBreakMode = UILineBreakModeWordWrap; 
     [cell addSubview:label]; 
     [label release]; 


    // [cell addSubview:imgview1]; 
    // [imgview1 release]; 

    } 

    if (indexPath.section ==1) { 

      if (indexPath.row == 0) 
     { 
      imgview = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 46, 46)]; 
      [cell addSubview:imgview]; 
      [imgview release]; 
      cell.textLabel.text = @"Actual Halal Rating"; 

      NSDictionary *aDict1 = [[NSDictionary alloc]init]; 
      aDict1 = [detailsArray objectAtIndex:0]; 

      NSString *statStr=[[NSString alloc] init]; 
      statStr = [aDict1 objectForKey:@"ProductHalalStatus"]; 
      NSLog(@"status is %@",statStr); 

      imgview1 = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 20, 20)]; 


      if ([statStr isEqualToString:@"Red"]) { 

       [imgview1 setImage:[UIImage imageNamed:@"Red.png"]]; 

      } 
      if ([statStr isEqualToString:@"Orange"]) { 

       [imgview1 setImage:[UIImage imageNamed:@"Yellow.png"]]; 

      } 
      if ([statStr isEqualToString:@"Green"]) { 


       [imgview1 setImage:[UIImage imageNamed:@"Green.png"]]; 

      } 
      [cell addSubview:imgview1]; 
      [imgview1 release]; 

     } 
     if (indexPath.row == 1) { 
      cell.textLabel.text = @"Halal (Permisible)"; 
      [imgview setImage:[UIImage imageNamed:@"Green.png"]]; 
     } 

     if (indexPath.row == 2) { 
      cell.textLabel.text = @"Masbooh (Doubtful)"; 
      [imgview setImage:[UIImage imageNamed:@"Yellow.png"]]; 
     } 

     if (indexPath.row == 3) { 
      cell.textLabel.text = @"Haram (Not Permisible)"; 
      [imgview setImage:[UIImage imageNamed:@"Red.png"]]; 
     } 

    } 


    if (indexPath.section == 2) { 


     NSDictionary *aDict2 = [[NSDictionary alloc]init]; 
     aDict2 = [detailsArray objectAtIndex:0]; 
    // NSArray *ingrArr =[aDict2 objectForKey:@"IngredientInfo1"]; 
     textview1 =[[UITextView alloc]initWithFrame:CGRectMake(12, 2, 294, 96)]; 
     //textview1.text = ingrStr; 
     textview1.editable =NO; 
     [textview1 setFont:[UIFont systemFontOfSize:15]]; 
     [cell addSubview:textview1]; 
     [textview1 release]; 

    } 


    if (indexPath.section == 3) { 


    } 
    return cell; 
} 

回答

3

刪除線,從你的代碼....

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

和下面的行

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease] 
+0

終止應用程序添加,由於未捕獲的異常 'NSInternalInconsistencyException',究其原因: 'UITableView dataSource必須從tableView返回一個單元格:cellForRowAtIndexPath:' – Anand 2011-04-29 06:16:49

+0

如果刪除它顯示上述錯誤 – Anand 2011-04-29 06:17:50

+0

檢查編輯後的ans我忘了添加一行代碼 – 2011-04-29 06:19:36