2013-07-19 21 views
0

我試圖在iOS應用程序中使用自定義UITableViewCell在UITableView中顯示結果。我能夠查看我的數組的內容,如果我使用單元格的textLabel和detailTextLabel屬性,但如果我嘗試鏈接UITableView與自定義單元類以顯示數組的內容,我只從默認值接口生成器顯示在我的表中。無法在iOS中查看自定義UITableViewCell

這是相關的代碼,我有:

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

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

    } 

    // Configure the cell... 


    TransactionList *tList = [_list objectAtIndex:indexPath.row]; 
/*  
    cell.transaction.text = tList.transaction; 
    cell.type.text = tList.type; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:@"ddMMYYYY"]; 

    NSString *dateAsString = [formatter stringFromDate:tList.date]; 
    cell.date.text = dateAsString; 

    */ 

//this code below displays the contents just fine, but the block of code above does not.  

    cell.textLabel.text = tList.transaction; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@", tList.type, tList.status]; 

    return cell; 
} 

下面的方法是我在我的CustomTableCell類:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 

     self.selectionStyle = UITableViewCellSelectionStyleNone; 

     self.contentView.frame = CGRectMake(0, 0, 320, 80); 

     _transaction = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 80, 40)]; 
     [_transaction setBackgroundColor:[UIColor clearColor]]; 
     [_transaction setFont:[UIFont systemFontOfSize:12]]; 
     [_transaction setNumberOfLines:2]; 
     [_transaction setLineBreakMode:NO]; 
     [self.contentView addSubview:_transaction]; 

     _date = [[UILabel alloc] initWithFrame:CGRectMake(100, 5, 80, 40)]; 
     [_date setBackgroundColor:[UIColor clearColor]]; 
     [_date setFont:[UIFont systemFontOfSize:12]]; 
     [_date setNumberOfLines:2]; 
     [_date setLineBreakMode:NO]; 
     [self.contentView addSubview:_date]; 

     _type = [[UILabel alloc] initWithFrame:CGRectMake(200, 5, 80, 40)]; 
     [_type setBackgroundColor:[UIColor clearColor]]; 
     [_type setFont:[UIFont systemFontOfSize:12]]; 
     [_type setNumberOfLines:2]; 
     [_type setLineBreakMode:NO]; 
     _type.lineBreakMode = NO; 
     [self.contentView addSubview:_type]; 


    } 
    return self; 
} 

凡_transaction,_DATE和_type是標籤。這裏是內部MainStoryboard我Interface Builder中的截圖:

enter image description here

我試圖從「基本」轉變作風,到「自定義」,到「字幕」,但這並不能幫助。它只顯示錶格的默認值,這些設置改變了它們的顯示方式,但是我的數組的內容沒有顯示在表格中。任何人都可以看到我做錯了什麼嗎?我真的認爲這個問題很簡單,但不幸的是我不能把它放在手上。

在此先感謝所有回覆的人。

回答

0

您不會在主視圖單元格中顯示3個對象。將「樣式」從「基本」更改爲「自定義」並放入對象。

在'Identity Inspector'中,確保將類重命名爲'CustomTableCell'。

視圖(事務,日期&類型)在CustomTableCell.h中有屬性,並且它們是否連接到IB中的'CustomTableCell'?

這裏有一個形象點出最後兩個:

enter image description here

+0

非常感謝您的回覆。如何以及從哪裏「放」到我的物體中?事務,日期和類型只是標籤,並在.h文件中聲明,但未連接到IB中的CustomTableCell。我猜他們需要成爲? – syedfa

+0

自宣佈之後,這很好。現在從對象庫中刪除對象(在我的例子中 - 2 UILabels,Master&Value)。然後,您可以通過控制拖動將它們連接到CustomTableCell。在你的情況下,@property定義左邊的圓應該是空的。從該圓圈拖動到CustomTableCell對象。應該出現3個屬性。選擇正確的一個。 – David

相關問題