Custom tableViewCell example with images and lables

删除回忆录丶 提交于 2020-02-23 06:46:26

问题


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:

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_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

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 ???


回答1:


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.




回答2:


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; 



回答3:


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?



来源:https://stackoverflow.com/questions/5273782/custom-tableviewcell-example-with-images-and-lables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!