我需要创建在应用程序中使用的自定义设置视图。该应用程序是一个模型,因此它实际上已经有一个模型,它使用 UITableViewCellStyleValue1,但没有任何内容是可编辑的。所以基本上我想要的是同样的东西,但是使用可编辑的 detailTextLabel (右侧的标签),最好的方法是什么?
5 回答
你可以继续使用 UITableViewCellStyleValue1。您需要为要编辑的每个单元格创建 UITextField 实例。然后将每个 UITextField 分配给适当的单元格的 accessoryView 属性。
您还可以通过以下方式匹配标签文本的颜色:
yourTextField.textColor = cell.detailTextLabel.textColor;
您可能需要摆弄 UITextField 实例的 frame 属性才能正确对齐。
我认为更简单的方法是使用 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;
好的,这是我现在的代码,没有显示 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;
}
您的文本字段未显示,因为您从未设置文本字段的框架。您需要设置具有正宽度和高度的框架。我将使用的高度是 43(行高 - 1px 灰线缓冲区)。
啊哈!好,我知道了!现在我要做的是通过查看单元格中左侧标题标签的宽度来动态定义详细信息文本字段的宽度。大多数时候我在模拟器中运行这个标题标签大小为0,但偶尔我会得到一个结果。这对我来说没有任何意义,我在想也许是因为细胞被重复使用了?你们有什么想法吗?
我在设置 textLabel 后添加了这段代码:
CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font];
然后在开头的案例块中,我这样做:
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];