我正在尝试编写一个原型UITableViewCell,其中包含我们在cellForRowAtIndexPath中设置的标题和未定义的UIControl:
现在我已经实现了单元格:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.control];
}
return self;
}
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
[_titleLabel setBackgroundColor:[UIColor clearColor]];
_titleLabel.numberOfLines = 2;
[_titleLabel setFont:[UIFont fontWithName:@"Georgia-Italic" size:15]];
[_titleLabel setTextColor:RGBColor(51, 102, 153)];
[_titleLabel setTextColor:[UIColor darkGrayColor]];
}
return _titleLabel;
}
- (UIControl *)control{
if (!_control) {
_control = [[UIControl alloc] init];
[_control setBackgroundColor:[UIColor whiteColor]];
}
return _control;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = [[self contentView] bounds];
self.titleLabel.frame = CGRectMake(10, 5, 85, contentRect.size.height-10);
self.control.frame = CGRectMake(100, 5, contentRect.size.width-105, contentRect.size.height-10);
[self.contentView bringSubviewToFront:self.control];
[self bringSubviewToFront: self.control];
}
并实现了cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellAddOrder";
ICControlCell *cell = (ICControlCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ICControlCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryNone];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
NSObject *cellContent = [self.cellList objectAtIndex:indexPath.row];
cell.titleLabel.text = ICLocalized([cellContent valueForKey:@"name"]);
switch ([[cellContent valueForKey:@"control"] intValue]) {
case 0: {
UITextField *textField = [[UITextField alloc] init];
textField.placeholder = ICLocalized([[cellContent valueForKey:@"name"] stringByAppendingString:@"_placeholder"]);
textField.delegate = self;
textField.returnKeyType = UIReturnKeyNext;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.text = [cellContent valueForKey:@"data"];
textField.tag = indexPath.row;
cell.control = textField;
}
break;
case 1:
case 2:
case 3: {
UISwitch *switchControl = [[UISwitch alloc] init];
[switchControl setOn:[[cellContent valueForKey:@"data"] boolValue]];
[switchControl addTarget:self
action:@selector(switchStateChanged:)
forControlEvents:UIControlEventValueChanged];
switchControl.tag = indexPath.row;
cell.control = switchControl;
}
break;
default:
break;
}
return cell;
}
但是当我试图测试单元格的控制器控件没有显示时,只是标题标签.. 我该如何处理这个问题?请帮帮我
答案 0 :(得分:1)
您应该为不同的项目使用不同的单元格标识符,例如,如果您有 UITextField和UISwitch ,那么您应该使用两个标识符,因为您是重复使用单元格。因此,它应该是重用的相同类型的单元格。
答案 1 :(得分:0)
Doc说:
您不能直接使用UIControl类来实例化控件。
因此,您可以将UIControl变量声明为单元格原型类,但将其设置为nil。所有与控件相关的操作都应该从委托内部完成,因为这是您确切知道要使用哪个类的地方。所以,
不要在“控件”的细胞类片段中初始化控件。属性:
- (UIControl *)control{
return _control;
}
Cell的构造函数也不应该调用addSubview来进行控制。所以,
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self.contentView addSubview:self.titleLabel];
}
return self;
}
在单元格的layoutSubviews中添加背景颜色设置:
- (void)layoutSubviews {
...
if (cell.control) {
[cell.control setBackgroundColor:[UIColor whiteColor]];
self.control.frame = CGRectMake(100, 5, contentRect.size.width-105, contentRect.size.height-10);
[self.contentView bringSubviewToFront:self.control];
[self bringSubviewToFront: self.control];
}
}
在委托的cellforrow方法中添加子视图:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...
cell.control = textField;
[cell.contentView addSubview:cell.control];
...
}