2013-08-31 148 views
0

我使用自定義表格單元格,標籤和圖像與標籤連接。setAccessoryType破壞TableViewCell

如果我添加

[細胞setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]

它破壞我的tableview。圖像破壞和背景顏色。

這裏的區別:

enter image description hereenter image description here

有沒有人有任何想法,問題可能是什麼? 感謝

編輯:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     if ([self labelCellNib]) { 
      [[self labelCellNib] instantiateWithOwner:self options:nil]; 
     } else { 
      [[NSBundle mainBundle] loadNibNamed:@"LabelCell" owner:self options:nil]; 
     } 

     cell = self.labelCell; 
     self.labelCell = nil; 
    } 

    static NSDateFormatter *dateFormatter = nil; 
    if (dateFormatter == nil) 
     dateFormatter = [[NSDateFormatter alloc] init]; 

    static NSCalendar *calendar; 
    if(calendar == nil) 
     calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 

    NSString * timeStampString = [sqlTime objectAtIndex:indexPath.row]; 
    NSTimeInterval _interval=[timeStampString doubleValue]; 
    NSDate *createdAt = [NSDate dateWithTimeIntervalSince1970:_interval]; 
    [dateFormatter setDateStyle:NSDateFormatterLongStyle]; 

    // Datum in die Tabellen-Zelle einfügen 
    UILabel *label4 = (UILabel *)[cell viewWithTag:LABEL4]; 
    label4.text = [NSString stringWithFormat:@"%@", [dateFormatter stringFromDate:createdAt]]; 

    // Configure the cell... 
    UILabel *label1 = (UILabel *)[cell viewWithTag:LABEL1]; 
    label1.text = [NSString stringWithFormat:@"%@ %@", [sqlData objectAtIndex:indexPath.row], sonderzeichen]; 

    UILabel *label2 = (UILabel *)[cell viewWithTag:LABEL2]; 
    label2.text = [NSString stringWithFormat:@"Preis pro %@: %@ €", sonderzeichen, [sqlPreis objectAtIndex:indexPath.row]]; 

    UILabel *label3 = (UILabel *)[cell viewWithTag:LABEL3]; 
    UIImageView *image = (UIImageView *)[cell viewWithTag:IMAGE_TAG]; 

    if (indexPath.row==[itemsArray count]-1) { 
     image.image = [UIImage imageNamed:@"default.png"]; 
     label3.text = @"-"; 
    } else if ([itemsArray count]>1) { 
     int data_old = [[NSString stringWithFormat:@"%@", [sqlData objectAtIndex:indexPath.row]] intValue]; 
     int data_new = [[NSString stringWithFormat:@"%@", [sqlData objectAtIndex:indexPath.row+1]] intValue]; 
     label3.text = [NSString stringWithFormat:@"%i", data_old-data_new]; 

     NSLog(@"%d -- %d", data_old, data_new); 
     if (data_new>data_old) { 
      image.image = [UIImage imageNamed:@"pfeil_runter.png"]; 
     } else if (data_new<data_old) { 
      image.image = [UIImage imageNamed:@"pfeil_hoch.png"]; 
     } else { 
      image.image = [UIImage imageNamed:@"minus.png"]; 
     } 
    } 

    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 

    cell.contentView.backgroundColor = indexPath.row % 2? [UIColor colorWithRed:228.0/255.0 green:244.0/255.0 blue:199.0/255.0 alpha:1]:[UIColor whiteColor]; 
    return cell; 
} 
+1

如何將標籤和圖像添加到您的單元格?請發佈cellForRowAtIndexPath。 – Joel

+0

我編輯了我的帖子 – Loko

+0

您應該將所有動態內容從cellForRowAtIndexPath移動到willDisplayCell。 – Joel

回答

2

您需要設置單元格的背景顏色,而不是內容查看,但這並不在工作的cellForRowAtIndexPath。它需要在willDisplayCell:forRowAtIndexPath:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = indexPath.row % 2? [UIColor colorWithRed:228.0/255.0 green:244.0/255.0 blue:199.0/255.0 alpha:1]:[UIColor whiteColor]; 
} 

至於圖像得到擠壓,一個與您的子視圖的大小,以及它們的約束做。我認爲你可以通過給你的兩個標籤(一對之間和之下的一個對)給予寬度約束來修復這些問題,但是使它們的優先級爲< 1000,所以當添加附件視圖時,這些標籤的寬度將變小。同時給左邊的圖像視圖一個固定的寬度。

+0

確定這與圖像運行!但背景顏色不適用於willDisplayCell代碼 – Loko

+0

@Loko,您將其更改爲cell.backgroundColor而不是cell.contentView.backgroundColor?我已經測試過了,它對我來說工作得很好。 – rdelmar

+0

@Loko,對不起,我有一個剪貼錯誤 - 我修復了它。 – rdelmar