2012-01-01 29 views
-1

我的目的是爲我的表格視圖的一行定製單元格。所以我做:爲單元格做定製但不工作

MyClass.m 
    1 .- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ 
    3 . NSLog(@"section %d:row %d",indexPath.section, indexPath.row); 
    4 . static NSString *CellIdentifier = @"reusedCell"; 
    5 . DetailCell *cell = (DetailCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    6 . cell.backgroundColor = [UIColor clearColor]; 
    7 . // Customize the cell of each row from table 
    8 . **if (cell == nil) { 
    9 .  cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    10. }** 
    11. return cell; 
    12.} 

我DetailCell類是

1.- (DetailCell*)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ 

    2. if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]){ 
    3. itemImg  = [[UIImageView alloc] initWithFrame:CGRectMake(20 , 20, 50 , 70)]; 
    4 itemName = [[UILabel alloc]  initWithFrame:CGRectMake(100, 0 , 100 , 50)]; 
    5. itemDesp = [[UILabel alloc]  initWithFrame:CGRectMake(100, 40, 1000, 20)]; 
    6 itemSize = [[UILabel alloc]  initWithFrame:CGRectMake(100, 60, 100 , 20)]; 
    7 itemPrice = [[UILabel alloc]  initWithFrame:CGRectMake(200, 60, 100 , 20)]; } 
    8. [itemImg setImage:[UIImage imageNamed:@"large/dresses/02/71EU6.png"]] ; 
    9. itemName.text = @"Dresses"; 
    10. itemDesp.text = @"Description"; 
    11. itemSize.text = @"S"; 
    12. itemPrice.text = @"$99"; 
    13. itemSize.textColor = [UIColor purpleColor]; 

    14. [self.contentView addSubview:itemImg]; 
    15. [self.contentView addSubview:itemName]; 
    16. [self.contentView addSubview:itemDesp]; 
    17. [self.contentView addSubview:itemSize]; 
    18. [self.contentView addSubview:itemPrice]; 
    19.  return self; 
    20. } 

要顯示在我的錶行的IMG或文字,我設置的UIImageView IMG +文字和DetailCell內標籤(第8行---> DetailCell中的第13行)。 但是,當我瀏覽一些閱讀材料時,我意識到人們使用DetailCell這樣的類來創建一個框架併爲UIImageView + Label添加屬性。最後,他們將在myClass.m內的單元格上顯示圖片或文字。 這令我感到困惑,因爲我們是否應該爲UIImageView設置img,位於MyClass.mDetailCell.m。 請指教我關於這個問題。謝謝。

回答

0

如果單元格不是零,它將不起作用。您通常會根據部分或行配置單元格。您想要自定義哪些單元格?假設你的第一個單元格將樣式像這樣和其他的不...

switch (indexPath.row) { 
    case 0: 
     cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault 
           reuseIdentifier:CellIdentifier]; 
     break; 
    default: 
     /* 
     * Configure the rest of the cells... 
     * 
     * Use case 1, 2, 3, 4, etc. as needed. 
     * You can then use default to set the default configuration 
     */ 
     break; 
} 

你可以嘗試這樣的方法來代替。此外請確保您沒有此項:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 1; 
} 

這是默認值,並且會限制您可以擁有的單元格數量。如果您有多個部分,請使用與前面示例中所用類似的switch語句。

+0

是的,我有一個像我的帖子在[鏈接]的問題http://stackoverflow.com/questions/8696295/issue-when-trying-to-create-my-own-cell_italic_**codepat'code '。我不知道爲什麼我的細胞第一次不是零。你的方法cellForRowAtIndexPath,儀式切換?爲什麼我們不應該有numberOfRowsInSection = 1。我認爲這不是一個問題,因爲你可以有多個行(或單元格)的1個部分。 – tonytran 2012-01-01 23:38:41

相關問題