有两种方法,一种是通过代码来实现,还有一种是通过xib来实现,好不容易找到个用代码来实现的:
转载自:http://blog.youkuaiyun.com/iorchid/article/details/6552387 (其实特简单,就是继承一个uitableviewcell类,然后在 reuseIdentifier 这个方法里实现就行,我是没有用到那个什么select方法)
这个是用xib来实现的,有空可以看看,网上有另一篇,一搜一大把,这儿就不罗列了:http://blog.youkuaiyun.com/dongstone/article/details/7438254
- 新建一个继承自UITableViewCell的类,CustomTableViewCell
- 在函数:- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier中,填写需要自定义的UI
- 比如:
- CustomTableViewCell.m
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
- {
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self)
- {
- // Initialization code
- _leftImageView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 100, 100)];
- _rightImageView = [[UIImageView alloc] initWithFrame: CGRectMake(170, 0, 100, 100)];
- self.backgroundColor = [UIColor redColor];
- //[self.contentView addSubview: _leftImageView];
- [self.contentView addSubview: _rightImageView];
- label = [[UILabel alloc] initWithFrame: CGRectMake( 0, 0, 60, 40)];
- [self.contentView addSubview: label];
- textField = [[UITextField alloc] initWithFrame: CGRectMake(60, 0, 100, 40)];
- textField.secureTextEntry = YES;
- [self.contentView addSubview: textField];
- }
- return self;
- }
- -(void) setImage2:(UIImage *)image
- {
- //_leftImageView.image = image;
- _rightImageView.image = image;
- }
- - (void) setText:(NSString *)text
- {
- label.text = text;
- }
- 在TableViewController的实现文件如下:
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"Cell";
- CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- {
- cell = (CustomTableViewCell *)[[[CustomTableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- // Configure the cell...
- // NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://www.baidu.com/img/baidu_sylogo1.gif"]];
- // UIImage *image = [UIImage imageWithData: data];
- //
- // [cell setImage2: image];
- //cell.textLabel.text = [array objectAtIndex: indexPath.row];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- cell.label.text = @"密码: ";
- return cell;
- }
- 自定义基本完成。