我需要创建应用中使用的自定义设置视图。该应用程序是一个模型,所以它实际上已经有一个,它使用UITableViewCellStyleValue1,但没有任何东西是可编辑的。基本上我想要的是同样的东西,但是使用可编辑的detailTextLabel(右侧的标签),最好的方法是什么?
答案 0 :(得分:2)
您可以继续使用UITableViewCellStyleValue1。您需要为要编辑的每个单元格创建UITextField实例。然后将每个UITextField分配给相应单元的accessoryView属性。
您还可以通过以下方式匹配标签文字的颜色:
yourTextField.textColor = cell.detailTextLabel.textColor;
您可能需要摆弄UITextField实例的frame属性以使对齐恰到好处。
答案 1 :(得分:2)
我认为更简单的方法是使用detailTextLabel显示文本并处理行选择代码中文本编辑字段的显示和隐藏。我有以下工作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITextField *tmpView = [self detailLabel:indexPath];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UILabel *detailLabel = [cell detailTextLabel];
CGRect tmpFrame = [detailLabel frame]; // the existing frame
tmpFrame.size.width += 3; // space for the cursor
tmpView.frame = tmpFrame;
tmpView.textColor = detailLabel.textColor;
cell.accessoryView = tmpView;
detailLabel.text = nil;
[tableView addSubview:tmpView];
[tmpView becomeFirstResponder];
[tableView setNeedsDisplay];
}
以及取消选择的以下内容
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITextField *tmpView = (UITextField *) cell.accessoryView;
UILabel *label = [cell detailTextLabel];
label.text = tmpView.text;
cell.accessoryView = nil;
[tableView setNeedsDisplay];
}
唯一的另一个技巧是将 cellForRowAtIndexPath 中的行选择突出显示为无...
cell.selectionStyle = UITableViewCellSelectionStyleNone;
答案 2 :(得分:0)
好的,所以这是我现在的代码,textField没有显示,我不知道为什么......
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
static NSString* reuseIdentifier = @"SettingsCell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if ( !cell )
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier] autorelease];
[cell setBackgroundColor:[UIColor baeDarkGrayColor]];
UILabel* cellLabel = [cell textLabel];
[cellLabel setTextColor:[UIColor whiteColor]];
[cellLabel setBackgroundColor:[UIColor clearColor]];
[cell setUserInteractionEnabled:YES];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}
// Populate cell with corresponding data.
NSDictionary* tableSection = [_tableData objectAtIndex:indexPath.section];
// Set the label
UILabel* textLabel = [cell textLabel];
NSString* labelString = [[tableSection objectForKey:@"itemTitles"] objectAtIndex:indexPath.row];
[textLabel setText:labelString];
// Set the detail string
// UILabel* detailLabel = [cell detailTextLabel];
// NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row];
// [detailLabel setText:detailLabelString];
// Set the accessory view
BAESettingsTableCellAccessoryType accessoryType = [[[tableSection objectForKey:@"itemAccessories"] objectAtIndex:indexPath.row] integerValue];
switch ( accessoryType )
{
case BAESettingsTableCellAccessoryDisclosureIndicator:
{
UIImageView* disclosureView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"AccessoryDisclosure.png"]];
[cell setAccessoryView:disclosureView];
[disclosureView release];
break;
}
case BAESettingsTableCellAccessoryOnOffSwitch:
{
UISwitch* onOffSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
[onOffSwitch setOn:YES];
[cell setAccessoryView:onOffSwitch];
[onOffSwitch release];
break;
}
case BAESettingsTableCellAccessoryNone:
// default:
// break;
{
UITextField* textField = [[UITextField alloc] init];
NSString* detailLabelString = [[tableSection objectForKey:@"itemDetailStrings"] objectAtIndex:indexPath.row];
textField.text = detailLabelString;
textField.textColor = [UIColor whiteColor];
[cell setAccessoryView:textField];
[textField release];
break;
}
}
return cell;
}
答案 3 :(得分:0)
您的文本字段未显示,因为您从未设置文本字段的框架。您需要设置具有正宽度和高度的框架。我将使用的高度是43(行高 - 1px灰线缓冲区)。
答案 4 :(得分:0)
啊哈!好,我知道了!现在我要做的是通过查看单元格中剩余的标题标签的宽度来动态定义详细信息文本字段的宽度。大多数时候我在模拟器中运行它,标题标签大小为0,但偶尔我得到一个结果。这对我没有任何意义,我想也许是因为细胞被重复使用了?你们有什么想法吗?
我在设置textLabel之后添加了这段代码:
CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font];
然后在开头的case块中我做了:
CGRect cellFrame = [cell frame];
float detailTextFieldWidth = cellFrame.size.width - ( textLabelSize.width + 50 );
float detailTextFieldHeight = cellFrame.size.height - 1;
NSLog(@"detail text field calculated frame width, height, %f, %f", detailTextFieldWidth, detailTextFieldHeight);
CGRect frame = CGRectMake(cellFrame.origin.x, cellFrame.origin.y, detailTextFieldWidth, detailTextFieldHeight);
UITextField* textField = [[UITextField alloc] initWithFrame:frame];