2012-06-29 33 views
4

我创建其中包含20个rows.I一个TableView有一个标签添加到偶数细胞,我必须在奇cells.When添加两个标签,我添加所需的标签和滚动我餐桌上的标签让我失望,因为我下去帮助我。标签上没有显示的tableView细胞

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

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


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    if(indexPath.row%2==0) 
    { 
     UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
     [email protected]"o1"; 
     [cell addSubview:cellLabel]; 
     [cellLabel release]; 
    } 
    else 
    { 
     UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
     [email protected]"e1"; 
     [cell addSubview:cellLabel1]; 
     [cellLabel1 release]; 
     UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)]; 
     [email protected]"e2"; 
     [cell addSubview:cellLabel2]; 
     [cellLabel2 release]; 
    } 

} 
+0

的可能重复的[在的UITableViewCell文本丢失时向下滚动(http://stackoverflow.com/questions/1432143/text-in-uitableviewcell -missing-时,向下滚屏) – rishi

+0

您需要tag'属性添加到''每一个的UILabel – iNoob

+0

http://www.theappcodeblog.com/2011/02/24/table-view-tutorial-part-4-customizing -a-table-cell/ –

回答

0

这两种细胞是不同的,所以我会为每个不同的小区标识符。例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *EvenCellIdentifier = @"EvenCell"; 
    static NSString *OddCellIdentifier = @"OddCell"; 
    NSString* cellIdentifier = (indexPath.row % 2) == 0 ? EvenCellIdentifier : OddCellIdentifier; 

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

     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     if(indexPath.row%2==0) 
     { 
      UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
      [email protected]"o1"; 
      [cell addSubview:cellLabel]; 
      [cellLabel release]; 
     } 
     else 
     { 
      UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
      [email protected]"e1"; 
      [cell addSubview:cellLabel1]; 
      [cellLabel1 release]; 
      UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)]; 
      [email protected]"e2"; 
      [cell addSubview:cellLabel2]; 
      [cellLabel2 release]; 
     } 
    } 

    return cell; 
} 
+0

我已经试过,但我再次向下滚动标签成为消失.... –

0

做这样的:

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



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

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

    UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
     [email protected]"o1"; 
cellLabel.tag = 2; 
     [cell addSubview:cellLabel]; 
     [cellLabel release]; 

     UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)]; 
     [email protected]"e1"; 
cellLable1.tag = 3; 
     [cell addSubview:cellLabel1]; 
     [cellLabel1 release]; 
     UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 40, 40, 40)]; 
     [email protected]"e2"; 
cellLabel2.tag = 4; 
     [cell addSubview:cellLabel2]; 
     [cellLabel2 release]; 
    } 

UILabel *label1 = (UILabel *) [cell viewWithTag:2]; 
UILabel *label2 = (UILabel *) [cell viewWithTag:3]; 
UILabel *label3 = (UILabel *) [cell viewWithTag:4]; 

if (indexPath.row%2==0) 
{ 
label1.hidden = FALSE; 
label2.hidden = TRUE; 
label3.hidden = TRUE: 
} 
else 
{ 
label1.hidden = TRUE; 
label2.hidden = FALSE; 
label3.hidden = FALSE; 
} 

    } 
+0

@RAjKumar ---- not worki NG。 –

+0

移除所有标签释放语句,然后再试一次,我相信它会工作 – Rajkumar

+0

由NNC一看便知上面... –

1

选中此项。这与您的代码相同,我只需更改每个标签的Y位置并使用滚动功能也可以正常工作。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

static NSString * CellIdentifier = @“Cell”;

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


    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    if(indexPath.row%2==0) 
    { 
     UILabel *cellLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 40, 40)]; 
     [email protected]"o1"; 
     [cell addSubview:cellLabel]; 

    } 
    else 
    { 
     UILabel *cellLabel1=[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 40, 40)]; 
     [email protected]"e1"; 
     [cell addSubview:cellLabel1]; 

     UILabel *cellLabel2=[[UILabel alloc]initWithFrame:CGRectMake(150, 10, 40, 40)]; 
     [email protected]"e2"; 
     [cell addSubview:cellLabel2]; 

    } 

} 
return cell; 

}

+1

如果你是使用默认行高,那么你的标签就会移出相对单元格。正如你已经给出Y = 40,默认行高为44. – Nikunj

+0

thanx !!!,它的工作原理! –

0
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *MyIdentifier = @"MyIdentifier"; 
    static NSString *MyIdentifier1 = @"MyIdentifier1"; 
    UITableViewCell *cell; 
    if(indexPath.row % 2 != 0) 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
    } 
    else 
    { 
     cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier1]; 
    } 
    if(cell == nil) 
    { 
     if(indexPath.row % 2 != 0) 
     { 
      cell=[self reuseTableViewCellWithIdentifier:MyIdentifier withIndexPath:indexPath]; 
     } 
     else 
     { 
      cell=[self reuseTableViewCellWithIdentifier:MyIdentifier1 withIndexPath:indexPath]; 
     } 
    } 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    return cell; 
} 
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease]; 
    if([identifier isEqualToString:@"MyIdentifier"]) 
    { 

     UILabel *l1=[[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 30, 20)]autorelease]; 
     l1.backgroundColor=[UIColor redColor]; 
     [cell.contentView addSubview:l1]; 
     UILabel *l2=[[[UILabel alloc]initWithFrame:CGRectMake(65, 5, 30, 20)]autorelease]; 
     l2.backgroundColor=[UIColor grayColor]; 
     [cell.contentView addSubview:l2];  
    } 
    else 
    { 
     UILabel *l1=[[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 30, 20)]autorelease]; 
     l1.backgroundColor=[UIColor blueColor]; 
     [cell.contentView addSubview:l1]; 
    } 
    return cell; 
} 
0
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexPath.row%2==0) 
    { 
    static NSString *CellIdentifier = @"CustomCell"; 

    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
     for (id currentObject in topLevelObjects) 
     { 
      if ([currentObject isKindOfClass:[UITableViewCell class]]) 
      { 
       cell = (CustomCell *) currentObject; 
       break; 
      } 
     } 
    } 
    cell.capitalLabel.text =[capitals objectAtIndex:indexPath.row]; 
    cell.stateLabel.text = [states objectAtIndex:indexPath.row]; 
    cell.t1.delegate=self; 
    cell.t1.tag=indexPath.row; 
    cell.t1.text=[arrTemp objectAtIndex:indexPath.row]; 
    cell.s1.backgroundColor=[UIColor grayColor]; 
    cell.s1.contentSize=CGSizeMake(1000, 40); 
    return cell; 
    } 
    else 
    { 
     static NSString *CellIdentifier1 = @"CustomCell1"; 

     CustomCell1 *cell1 = (CustomCell1 *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; 
     if (cell1 == nil) 
     { 
      NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell1" owner:self options:nil]; 
      for (id currentObject in topLevelObjects) 
      { 
       if ([currentObject isKindOfClass:[UITableViewCell class]]) 
       { 
        cell1 = (CustomCell1 *) currentObject; 
        break; 
       } 
      } 
     } 
     [email protected]"alok"; 
     return cell1; 

    } 

} 

0
 static NSString *CellIdentifier = @"Cell"; 
    static NSString *CellIdentifierButton = @"CellButton"; 
    UITableViewCell *cell = nil; 




     if (indexPath.section <=2) 
     { 
     if(indexPath.section==2&&indexPath.row==4) 
     { 
      cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierButton]; 

     } 
     else 
     { 
      cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     } 
    } 
    if (cell == nil) 
    { 
     if (indexPath.section <=2) 
     { 
      if(indexPath.section==2&&indexPath.row==4) 
      { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifierButton]; 
       UIButton *b1 = [[UIButton alloc]initWithFrame:CGRectMake(200, 10, 300, 80)]; 
       [b1 setBackgroundColor:[UIColor redColor]]; 
       [b1 setTag:501];   
       [cell.contentView b1]; 

       UIButton * b2 = [[UIButton alloc]initWithFrame:CGRectMake(200, 120, 300, 80)]; 
       [b2 setBackgroundColor:[UIColor redColor]]; 
       [b2 setTag:502];   
       [cell.contentView b2]; 

      } 
      else 
      { 
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
       UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 250, 44)]; 
       [label setBackgroundColor:[UIColor clearColor]]; 
       [label setTag:500];   
       [cell.contentView addSubview:label]; 

       //end for section 1 

       //for section2 



       } 
       //end for section 2 

      } 


     } 
    } 




    //====setting the values===// 
    if (indexPath.section ==0) 
    { 
     UILabel *label = (UILabel *)[cell.contentView viewWithTag:500]; 
     if (label) { 
      [label setText:[arr1 objectAtIndex:indexPath.row]]; 
     } 
    } 
    if (indexPath.section ==1) 
    { 
     UILabel *label = (UILabel *)[cell.contentView viewWithTag:500]; 
     if (label) { 
      [label setText:[arr2 objectAtIndex:indexPath.row]]; 
     } 
    }