2011-03-11 58 views
0

我開始創建它之前創建自定義UITableViewCell我閱讀了許多關於它的文章,並開始創建我自己的CustomTableViewCell。自定義tableViewCell圖像和標籤示例

在我的自定義TableViewCell我有4個filds:

  1. 的UILabel * cellTitle
  2. 的UILabel * cellDateTime
  3. 的UIView * cellMainImage
  4. 的UIImageView * arraow圖像

這裏是如何是我的TableViewCell出現: enter image description here

這裏是代碼:CustomTableViewCell.h

#import <UIKit/UIKit.h> 

#define TAGS_TITLE_SIZE  20.0f 
#define TITLE_LABEL_TAG  1 
#define DATA_TIME_LABEL_TAG 5 
#define ARROW_IMAGE_TAG  6 
#define MAIN_IMAGE_TAG  7 

// Enumeration for initiakization TableView Cells 
typedef enum { 
    NONE_TABLE_CELL  = 0, 
    NEWS_FEED_TABLE_CELL = 1, 
    TAGS_TABLE_CELL  = 2 
}TableTypeEnumeration; 

// Class for Custom Table View Cell. 
@interface CustomTableViewCell : UITableViewCell { 
    // Title of the cell. 
    UILabel*  cellTitle; 
    UILabel*  cellDataTime; 
    UIView*  cellMainImage; 
    UIImageView* cellArrowImage; 
} 

// Set the title of the cell. 
- (void) SetCellTitle: (NSString*) _cellTitle; 
- (void) SetCellDateTime: (NSString*) _cellDataTime; 

- (void) ReleaseCellMainImage; 

- (void) InitCellTitleLable; 
- (void) InitCellDateTimeLabel; 
- (void) InitCellMainImage; 



// Init With Style (With additional parametr TableTypeEnumeration) 
- (id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier tableType:(TableTypeEnumeration)tabletypeEnum; 

@end 

這裏是代碼:CustomTableViewCell.m

#import "CustomTableViewCell.h" 

@implementation CustomTableViewCell 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    return [self initWithStyle:style reuseIdentifier:reuseIdentifier tableType:NONE_TABLE_CELL]; 
} 

- (id)initWithStyle: (UITableViewCellStyle)style reuseIdentifier: (NSString *)reuseIdentifier tableType:(TableTypeEnumeration)tabletypeEnum { 
    // Get Self. 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     // Switch table View Cells 
     switch(tabletypeEnum) { 
      case NEWS_FEED_TABLE_CELL: { 

       // Create Cell Title Text 
       cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(75.0f, 2.5f, 180.0f, 33.0f)]; 
       cellTitle.tag   = TITLE_LABEL_TAG; 
       cellTitle.font   = [UIFont boldSystemFontOfSize: 13.0f]; 
       cellTitle.lineBreakMode = UILineBreakModeWordWrap; 
       cellTitle.numberOfLines = 0; 
       cellTitle.textAlignment = UITextAlignmentLeft; 
       cellTitle.textColor  = [UIColor blackColor]; 
       [self.contentView addSubview:cellTitle]; 
       [cellTitle release]; 

       // Create Cell Description Text. 
       cellDataTime = [[UILabel alloc] initWithFrame:CGRectMake(135.0f, 38.0f, 100.0f, 15.0f)]; 
       cellDataTime.tag   = DATA_TIME_LABEL_TAG; 
       cellDataTime.font   = [UIFont italicSystemFontOfSize: 12.0f]; 
       cellDataTime.textAlignment = UITextAlignmentLeft; 
       cellDataTime.textColor  = [UIColor blackColor]; 
       cellDataTime.lineBreakMode = UILineBreakModeWordWrap; 
       [self.contentView addSubview:cellDataTime]; 
       [cellDataTime release]; 

       // Create Cell Arrow Image. 
       cellArrowImage = [[UIImageView alloc] initWithFrame:CGRectMake(260.0f, 7.0f, 40.0f, 49.0f)]; 
       cellArrowImage.tag    = ARROW_IMAGE_TAG; 
       cellArrowImage.backgroundColor = [UIColor whiteColor]; 
       cellArrowImage.image   = [UIImage imageNamed:@"Grey Arrow.png"];; 
       [self.contentView addSubview:cellArrowImage]; 
       [cellArrowImage release]; 

       // Create Cell Main Image. 
       cellMainImage = [[[UIView alloc] initWithFrame:CGRectMake(2.0f, 2.5f, 55.0f, 50.0f)] autorelease]; 
       cellMainImage.tag = MAIN_IMAGE_TAG; 
       [self.contentView addSubview:cellMainImage]; 


       break; 
      } 
      case TAGS_TABLE_CELL: { 

       // Create and initialize Title of Custom Cell. 
       cellTitle = [[UILabel alloc] initWithFrame:CGRectMake(10, (44 - TAGS_TITLE_SIZE)/2, 260, 21)]; 
       cellTitle.backgroundColor  = [UIColor clearColor]; 
       cellTitle.opaque    = NO; 
       cellTitle.textColor   = [UIColor blackColor]; 
       cellTitle.highlightedTextColor = [UIColor whiteColor]; 
       cellTitle.font     = [UIFont boldSystemFontOfSize:TAGS_TITLE_SIZE]; 
       cellTitle.textAlignment  = UITextAlignmentLeft; 
       [self.contentView addSubview:cellTitle]; 
       [cellTitle release]; 

       break; 
      } 
      default: break; 
     } 
    } 
    return self; 


} 

- (void) ReleaseCellMainImage { 
    [cellMainImage release]; 
} 

- (void) InitCellTitleLable { 
    cellTitle = (UILabel *)[self.contentView viewWithTag:TITLE_LABEL_TAG]; 
} 

- (void) InitCellDateTimeLabel { 
    cellDataTime = (UILabel *)[self.contentView viewWithTag:DATA_TIME_LABEL_TAG]; 
} 

- (void) InitCellMainImage { 
    //UIView* oldImage = [self.contentView viewWithTag:MAIN_IMAGE_TAG]; 
    //[oldImage removeFromSuperview]; 
} 


- (void) SetCellTitle: (NSString*) _cellTitle { 
    cellTitle.text = _cellTitle; 
} 


- (void) SetCellDateTime: (NSString*) _cellDataTime { 
    cellDataTime.text = _cellDataTime; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
    [super setSelected:selected animated:animated]; 
} 


- (void)dealloc { 

    // Call base delloc 
    [super dealloc]; 
} 

@end 

現在,當我用我的CustomTableViewCell在程序的代碼我的iPhone的記憶力總是上升!每當我打開tableView的內存增長爲2MB,當我打開和關閉tableView 10次它變得更多然後30mb! Whot可以嗎?

還有一個問題

我怎樣才能得到事件時用戶爲我定製的細胞圖像上例如按???

+0

你不需要爲這個子類UITableViewCell ...檢查了這一點:http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing。html – GendoIkari 2011-03-11 14:01:13

回答

0

除了像其他人所說的那樣考慮單元重用,如果內存使用隨着每次打開而增加,則可能會發生內存泄漏。也許您創建表的視圖在釋放時不會釋放它。

0

您並未重複使用您的單元格。因此,每次滾動時都會創建一個新的單元格。

在您的代理需要重新創建細胞如下:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (nil == cell) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:CellIdentifier] autorelease]; 
} 



return cell; 
+0

是的我喜歡這樣CustomTableViewCell * cell =(CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(nil == cell){cell [= [CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } return cell; – 2011-03-11 14:03:18

+0

autorelease它。我也錯過了。 – 2011-03-11 14:08:55

0

創建的細胞,當你實現電池再利用?在代碼中沒有任何跡象表明你在init之前試圖從UITableView出列一個可重用的單元,儘管這可能在表視圖本身中。正如在Praveens文章中可以看到的那樣,有一個嘗試將一個單元出隊,並且只有當這個返回nil時該單元才被初始化。

因此,每當此特定單元格進入視圖時,您可能正在創建一個新的單元對象。表格視圖中採用了單元重用嗎?

tableview委託方法中的代碼是什麼 - tableView:cellForRowAtIndexPath?