2010-12-06 44 views
0

我想在我的tableview每行上有兩個數據。 喜歡的東西最近調用(左邊的名字,右邊的一天)UITableview每行兩個數據

這是我的代碼:

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease]; 
    //cell.selectionStyle = UITableViewCellStyleValue1 ; 
} 

// Configure the cell. 

NSDictionary *dictionary = [listaOggetti objectAtIndex:indexPath.section]; 
NSArray *array = [dictionary objectForKey:@"Elementi"]; 
NSString *cellValue = [array objectAtIndex:indexPath.row]; 
NSArray *array1 = [dictionary objectForKey:@"Tipo"]; 
NSString *cellDetail = [array1 objectAtIndex:indexPath.row]; 

if ([indexPath row] == 0){ 
    cell.textLabel.text = cellValue; 
    cell.textAlignment = UITextAlignmentCenter; 
    cell.backgroundColor = [UIColor blueColor]; 
    cell.font = [UIFont systemFontOfSize:13]; 
} else { 

cell.textLabel.text = cellDetail; 
cell.detailTextLabel.text = cellValue; 
} 

return cell; 
} 

我已經嘗試上使用不同的風格

initWithStyle 

沒有任何結果(只顯示我的詳細信息在右邊)。 在NSLOG中,我看到了cellDetail的正確值。

{ 
    Elementi =   (
     "Chiamate e Videochiamate Nazionali", 
     "08m:43s", 
     "1h:31m:17s", 
     "1h:40m:00s" 
    ); 
}, 
    { 
    Tipo =   (
     Effettuato, 
     Rimanente, 
     Totale 
    ); 
}, 
    { 
    Elementi =   (
     "SMS e MMS Nazionali", 
     21, 
     29, 
     50 
    ); 
}, 
    { 
    Tipo =   (
     Effettuato, 
     Rimanente, 
     Totale 
    ); 
}, 
    { 
    Elementi =   (
     "Traffico Dati FMM", 
     "69.95 MB", 
     "954.05 MB", 
     "1024.00 MB" 
    ); 
}, 
    { 
    Tipo =   (
     Effettuato, 
     Rimanente, 
     Totale 
    ); 
}, 
    { 
    Elementi =   (
     Skype, 
     "00m:00s", 
     "3h:20m:00s", 
     "3h:20m:00s" 
    ); 
}, 
    { 
    Tipo =   (
     Effettuato, 
     Rimanente, 
     Totale 
    ); 
}, 
    { 
    Elementi =   (
     "WWW3, Yahoo!, Google, eBay e altri servizi", 
     "0.00 MB", 
     "100.00 MB", 
     "100.00 MB" 
    ); 
}, 
    { 
    Tipo =   (
     Effettuato, 
     Rimanente, 
     Totale 
    ); 
} 
) 

我的目標是獲得細胞是這樣的:

alt text

回答

1

我會考慮creating a custom Cell,它很容易做到,如果做得好,它看起來真的很棒。

否則,您的代碼看起來不錯,請檢查您的標籤是否已被分配(而不是零)。

請注意,您的數據集中有多少項?我只問,因爲如果它只有1,你從來沒有設置detailTextLabel。

1

我相信你需要初始化這樣的細胞,要初始化細胞

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
          reuseIdentifier:CellIdentifier] autorelease]; 
在使用不正確的風格
+0

`dequeueReusableCellWithIdentifier`返回一個指向一個可重複使用的單元格或零,而他之後和allocs檢查零如果合適 – 2010-12-06 14:55:20

+0

@Luke Mcneice我相信初始化細胞 – 2010-12-06 15:01:26

+0

OK,用於校正感謝時,他使用了錯誤的風格 – 2010-12-06 15:09:09

0

謝謝大家,我的代碼沒問題,故障在第二個數組上。 解決了這種方式:

NSDictionary *dictionary1 = [listaDettaglioOggetti objectAtIndex:indexPath.section]; 
NSArray *array1 = [dictionary1 objectForKey:@"Tipo"]; 
NSString *cellDetail = [array1 objectAtIndex:indexPath.row]; 

最後一個問題:

我將修改每一節的第一行(修改字體大小,顏色,aligment)。 我已經覺得是這樣的:

NSInteger sectionRows = [tableView numberOfRowsInSection:[indexPath section]]; 
    if (sectionRows == 0){ 
    cell.textLabel.text = cellDetail; 
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 
    cell.detailTextLabel.backgroundColor = [UIColor blueColor]; 
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13]; 
    } else { 
cell.textLabel.text = cellDetail; 
cell.detailTextLabel.text = cellValue; 
} 

但不要讓魔法:d 請讓我知道,如果它是更好地打開另一個話題,或者我們可以繼續在這裏。

相關問題