Introduce two methods
. The first is to
simply add some small functions to UITableViewCell,
such as adding a UILabel to the cell.
Implemented directly in the generation method of UITableViewCell, the code is as follows
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier ];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(190, 0, 130, cell.frame .size.height)];
label1.tag = 1;
[cell.contentView addSubview:label3];
[label3 release];
}
UILabel *label3 = (UILabel *)[cell.contentView viewWithTag:1];
label1.text = @"44444";
return cell;
}
The second, more formal way of writing.
Create a new custom class that inherits UITableViewCell such as NewCell.
Add two UILabel properties in NewCell The
code is as follows
//NewCell.h
#import
@interface NewCell : UITableViewCell
{
UILabel *_label1;
UILabel *_label2;
}
- (void)setLabel1Text:(NSString *)text1
label2Text:(NSString *) text2;
@end
//NewCell.m
#import "NewCell.h"
@implementation NewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 160, self.frame.size.height)];
_label1.text = @"111111111";
[self.contentView addSubview:_label1];
_label2 = [[UILabel alloc] initWithFrame:CGRectMake(160, 0, 160, self.frame.size.height)];
_label2.text = @"111111111";
[self.contentView addSubview:_label2];
}
return self;
}
- (void)setLabel1Text:(NSString *)text1
label2Text:(NSString *)text2
{
_label1.text = text1;
_label2.text = text2;
}
- (void)dealloc
{
[_label1 release];
[_label2 release];
[super dealloc];
}
@end
//UITableViewCell的生成方法
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
NewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[NewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
}
[cell setLabel1Text:@"222222222" label2Text:@"333333333"];
return cell;
}
Pure code to implement custom extension of UITableViewCell
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326854140&siteId=291194637
Recommended
Ranking