【问题标题】:Custom tableViewCell example with images and lables带有图像和标签的自定义 uitableViewCell 示例
【发布时间】:2011-07-13 12:38:16
【问题描述】:

在开始创建自定义 UITableViewCell 之前,我正在创建它,我阅读了很多关于它的文章并开始创建自己的 CustomTableViewCell。

在我的自定义 TableViewCell 中,我有 4 个字段:

  1. UILabel* 单元格标题
  2. UILabel* 单元格日期时间
  3. UIView* cellMainImage
  4. UIImageView* 箭头图像

这是我的 TableViewCell 的显示方式:

这里是代码: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 !我能做什么???

还有一个问题

当用户例如在自定义单元格中按下我的图像时如何获取事件???

【问题讨论】:

标签: iphone image label cell


【解决方案1】:

除了像其他人所说的考虑单元重用之外,如果每次打开时内存使用量都增加,则可能存在内存泄漏。也许您创建表的视图在释放时不会释放它。

【讨论】:

    【解决方案2】:

    您没有重复使用您的细胞。因此,每次滚动都会创建一个新单元格。

    在您的委托中,您需要重新创建单元格,如下所示:

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

    【讨论】:

    • 是的,我喜欢这个 CustomTableViewCellcell = (CustomTableViewCell)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (nil == cell) { cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } 返回单元格;
    • 自动释放它。我也错过了。
    【解决方案3】:

    您是否在创建单元格时实现了单元格重用?代码中没有任何迹象表明您尝试在初始化之前从 UITableView 出列可重用单元格,尽管这可能在表格视图本身中。从 Praveens 的帖子中可以看出,尝试将单元格出列,并且只有当它返回 nil 时才会初始化单元格。

    因此,您可能会在每次看到这个特定单元格时创建一个新的单元格对象。表格视图是否采用单元格复用?

    tableview 委托方法中的代码是什么 - tableView:cellForRowAtIndexPath?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多