首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >带有图像和标签的自定义tableViewCell示例

带有图像和标签的自定义tableViewCell示例
EN

Stack Overflow用户
提问于 2011-03-11 13:55:05
回答 3查看 3.2K关注 0票数 0

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

在我的自定义TableViewCell中,我有4个文件:

cellTitle

  • UILabel* cellDateTime

  • UIView* cellMainImage

  • UIImageView* arraow图像

下面是我的TableViewCell是如何出现的:

这是代码: of CustomTableViewCell.h

代码语言:javascript
运行
复制
#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

代码语言:javascript
运行
复制
#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次时,内存就会超过30 2mb!我能做什么??

和另一个问题

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-03-11 14:19:54

除了像其他人说的那样考虑单元重用之外,如果内存使用随着每次打开而增加,您可能会有内存泄漏。也许您所拥有的创建表的视图在解除分配时不会释放它。

票数 0
EN

Stack Overflow用户

发布于 2011-03-11 14:00:44

你不能重复使用你的细胞。因此,每次滚动时都会创建一个新单元格。

在您的委托中,您需要按以下方式重新创建单元格:

代码语言:javascript
运行
复制
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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



return cell; 
票数 0
EN

Stack Overflow用户

发布于 2011-03-11 14:15:26

在创建单元时,是否实现了单元格的重用?代码中没有迹象表明您试图在init之前将可重用单元从UITableView中排出队列,尽管这可能在表视图本身中。在Praveens的文章中可以看到,有人试图将单元格排出队列,并且只有在返回零的情况下,才会初始化单元格。

因此,每次该特定单元格出现时,您都可能创建一个新的单元格对象。在表视图中是否采用了单元重用?

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5273782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档