2013-09-27 53 views
2

我没有iOS6的这个问题,但我目前正在使用iOS7。我有一个UITableView,你可以在加载视图时看到8个单元格。每个人都使用和数组中的不同名称进行填充。如果向下滚动,接下来的两个单元格看起来不错,但过去的所有内容都会将文本放在顶部;该文本是以前单元格中的内容。所以第10个单元格将具有第一个单元格中的内容,以及第10个单元格中应该放置的内容。UITableView单元内的UILabels在iOS7中彼此重叠

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     static NSString *CellIdentifier = @"Cell"; 
     UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

     if (cell == nil) 
     { 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 

     //Create Label for Name inside cell 
     UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5.0, 300.0, 30.0)]; 
     [name setText:[self.entriesArray objectAtIndex:indexPath.row]]; 


     //Check to see if current person is a Parent or Child 
     NSString *class = [self.database getCellDataWithMembership:[self.MembershipArray objectAtIndex:indexPath.row] andColIndex:4]; 

     if([class isEqualToString:@"CHILD"]) 
     { 
      name.textColor = [UIColor colorWithRed:25.0f/255.0f green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f]; 
      name.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];     
     } 

     [cell.contentView addSubview:name]; 
     return cell; 
} 

我与表视图技能是临时的最好的。我一直在阅读大量文档和研究解决方案,但无法提出解决方案。我觉得很奇怪,它适用于iOS6,但不适用于iOS7。

因此,它从一个数组中提取一个人的名字,我想用这些名字填充单元格。我能原本做到这一点使用:

cell.textLabel.text = [self.entriesArray objectAtIndex:indexPath.row]; 

if([class isEqualToString:@"CHILD"]) 
{ 
    cell.textLabel.textColor = [UIColor colorWithRed:25.0f/255.0f green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f]; 
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0]; 

如果我用这个来代替“名称”的UILabel从第一个代码块的行为,那么它完全没有文字叠加显示的名字,但什么成为一个问题是文字颜色。如果他们被标记为儿童,那么他们应该是绿色文字和粗体。但是,滚动后,每个人都不会变绿。

对不起,冗长的问题。我一直在研究这个问题,并围绕它大肆渲染我的大脑,我似乎无法弄清楚。任何帮助将不胜感激。

回答

-1

您可以通过使用ios6/7 delta来设置uilabels。首先通过更改x,y,宽度,高度值来设置ios7的总视图,然后在ios6/7 delta中更改值以使其适用于ios6 。希望你得到

+0

很抱歉,但是这无关我的问题。我知道你在说什么,但这是无关紧要的。我有一个UITableView中的单元格,它们基本上是相互重复的。 Delta的值可能不会解决这个问题:( – lshaffer

-2

你可以改变这段代码。

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; -> UITableViewCell *cell = nil; 
+0

我曾尝试过,但问题是用户需要点击单元格的能力,程序需要知道选择了哪个单元格行,以便它可以从中获取该索引当它为零时,它不再叠加,但是你不能再点击单元格以带你到下一个视图 – lshaffer

+0

如果增加了任何额外的细节,通过模式的segue可以完成到另一个视图的segue – lshaffer

0

在您的代码中,每当调用cellForRowAtIndexPath:方法时,都会将标签添加到单元格中。我的意思是你多次添加标签。您可以删除之前添加的标签。

//If label was added with tag = 500, remove it 
for (UIView *sv in cell.contentView.subviews) 
{ 
    if (sv.tag == 500) 
    { 
     [sv removeFromSuperview]; 
    } 
} 

//Create Label for Name inside cell 
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5.0, 300.0, 30.0)]; 
[name setText:[self.entriesArray objectAtIndex:indexPath.row]]; 
name.tag = 500; 

但是,此解决方案不是一个好的解决方案。我认为创建一个自定义UITableViewCell是正确的做法。

1

我在动态添加UI对象到单元格。作为“caglar”笔记,添加和删除不是最佳做法。但是,我也在这样做。我每次在单元中添加大量UI对象时,都会首先删除所有子视图。 willDisplayCell委托然后将它们添加回来。很明显,如果你只想删除某些视图,你必须标记视图,并选择删除。但是您可以使用以下方式删除所有内容。

-(void)tableView:(UITableView *)tableView 
willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath { 

    [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 
} 

只要确保您将标签添加/删除到单元格的contentView。在iOS7中,如果您不小心,也会将其删除。

希望它可以帮助你。

0

你可以试试这个方法,当cellnil,则需要creare的UILabel* name,然后设置name标签与标签name.tag = 1000,那么你可以用这个方法UILabel* name = (UILabel*)[cell.contentView viewWithTag:1000];访问此标签。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

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

     //Create Label for Name inside cell 
     UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(7.0, 5.0, 300.0, 30.0)]; 
     name.tag = 1000; 
     [cell.contentView addSubview:name]; 
    } 

    UILabel* name = (UILabel*)[cell.contentView viewWithTag:1000]; 
    [name setText:[self.entriesArray objectAtIndex:indexPath.row]]; 


    //Check to see if current person is a Parent or Child 
    NSString *class = [self.database getCellDataWithMembership:[self.MembershipArray objectAtIndex:indexPath.row] andColIndex:4]; 

    if([class isEqualToString:@"CHILD"]) 
    { 
     name.textColor = [UIColor colorWithRed:25.0f/255.0f green:111.0f/255.0f blue:45.0f/255.0f alpha:1.0f]; 
     name.font = [UIFont fontWithName:@"Helvetica-Bold" size:17.0];     
    } 

    return cell; 

}