开发者

Custom tableViewCell example with images and lables

开发者 https://www.devze.com 2023-02-16 10:22 出处:网络
I am creating custom UITableViewCell before starting to create it i read many articles about it and I start to create my own CustomTableViewCell.

I am creating custom UITableViewCell before starting to create it i read many articles about it and I start to create my own CustomTableViewCell.

In my custom TableViewCell I have 4 filds:

  1. UILabel* cellTitle
  2. UILabel* cellDateTime
  3. UIView* cellMainImage
  4. UIImageView* arraow image

Here is how is my TableViewCell appear:

Custom tableViewCell example with images and lables

And here is the code: of 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_开发者_如何学GoIMAGE_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

And here is the code of: 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

Now when I use my CustomTableViewCell in the code of the program the memory of my iphone always go up !!! Every time when I open tableView the memory grows for 2mb and when I open and close tableView for 10times it become more then 30mb !!! Whot can I do ???

And one more question

How I can get the event when user for example press on my image in custom cell ???


In addition to considering cell reuse as others say, if the memory use goes up with each open, you may have a memory leak. Perhaps the view you have that creates the table is not releasing it when deallocated.


You aren't reusing your cells. Hence a new cell is created everytime you scroll.

In your delegate you need to recreate cell as follows:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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



return cell; 


Are you implementing cell re-use when creating the cell? There is no indication in the code that you attempt to dequeue a reusable cell from UITableView prior to the init, though this may be in the table view itself. As can be seen in Praveens post, there is an attempt to dequeue a cell, and only if this returns nil is the cell being initialised.

As a result, you may be creating a new cell object every time this particular cell comes into view. Is cell reuse adopted in the table view?

What code is in the tableview delegate method - tableView:cellForRowAtIndexPath?

0

精彩评论

暂无评论...
验证码 换一张
取 消