2013-09-27 56 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; 

}