UITableView 에서 입력가능한 Cell 만들기 (CWTextInputTableViewCell class)
IB에서 일반 View를 생성하여 UITextField를 추가하여 정보를 입력 받는 방식만 써오다가
//
// CWTextInputTableViewCell.h
// RoadDesigner
//
// Created by Changwook Jeong on 11. 3. 4..
// Copyright 2011 GNR Technology Inc. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CWTextInputTableViewCell : UITableViewCell {
UILabel *_mainLabel;
UITextField *_inputField;
}
@property (nonatomic, retain) UILabel *mainLabel;
@property (nonatomic, retain) UITextField *inputField;
@end
//
// CWTextInputTableViewCell.m
// RoadDesigner
//
// Created by Changwook Jeong on 11. 3. 4..
// Copyright 2011 GNR Technology Inc. All rights reserved.
//
#import "CWTextInputTableViewCell.h"
@implementation CWTextInputTableViewCell
@synthesize mainLabel = _mainLabel;
@synthesize inputField = _inputField;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
_inputField = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 0.0, self.bounds.size.width-30.0, self.bounds.size.height)];
_inputField.font = [UIFont systemFontOfSize:17.0];
_inputField.textAlignment = UITextAlignmentLeft;
_inputField.textColor = [UIColor blackColor];
_inputField.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
_inputField.leftViewMode = UITextFieldViewModeAlways;
_inputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_inputField.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.contentView addSubview:_inputField];
_mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 90.0, self.bounds.size.height)];
_mainLabel.font = [UIFont systemFontOfSize:17.0];
_mainLabel.textAlignment = UITextAlignmentLeft;
_mainLabel.adjustsFontSizeToFitWidth = YES;
_mainLabel.textColor = [UIColor blackColor];
//_mainLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleHeight;
[_inputField setLeftView:_mainLabel];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
- (void)dealloc {
[_mainLabel release];
[_inputField release];
[super dealloc];
}
@end
* 사용할때는 아래와 같이 사용하면 된다.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
CWTextInputTableViewCell *cell =
(CWTextInputTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[[CWTextInputTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
cell.mainLabel.text = @"mainLabel";
return cell;
}