如何创建自定义设置视图/使单元格标签可编辑

发布于 2024-08-15 08:59:32 字数 154 浏览 7 评论 0原文

我需要创建应用程序中使用的自定义设置视图。该应用程序是一个模型,因此它实际上已经有了一个模型,它使用 UITableViewCellStyleValue1,但没有任何内容是可编辑的。所以本质上我想要的是同样的东西,但是使用可编辑的detailTextLabel(右侧的标签),最好的方法是什么?

I need to create a custom settings view used within the app. The app is a mockup so it actually does have one already, it uses a UITableViewCellStyleValue1, but nothing is editable. So essentially what I would like to have is the same thing but with editable detailTextLabel (the label on the right hand side), what's the best way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

灯下孤影 2024-08-22 08:59:32

您可以继续使用 UITableViewCellStyleValue1。您需要为每个想要编辑的单元格创建 UITextField 实例。然后,将每个 UITextField 分配给相应单元格的accessoryView 属性。

您还可以通过以下方式匹配标签文本的颜色:

yourTextField.textColor = cell.detailTextLabel.textColor;

您可能需要调整 UITextField 实例的frame 属性以获得正确的对齐方式。

You can continue to use UITableViewCellStyleValue1. You'll need to create UITextField instances for each cell you want editable. Then you assign each UITextField to the appropriate cell's accessoryView property.

You can also match the color of the label text via:

yourTextField.textColor = cell.detailTextLabel.textColor;

You may need to fiddle with the frame property of the UITextField instances to get the alignment just right.

诗酒趁年少 2024-08-22 08:59:32

我认为更简单的方法是使用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;

I think that the easier way of doing this is to use the detailTextLabel to show the text and handle the show and hide of the text edit fields in the row selection code. I have this working with the following:


- (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];
}

and the following on the de-select


- (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];
}

The only other trick was to set the row selection highlighting in the cellForRowAtIndexPath to none...

  cell.selectionStyle = UITableViewCellSelectionStyleNone;
探春 2024-08-22 08:59:32

好的,这是我现在的代码,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;

}

OK, so here's the code I have now, the textField is not displayed, I'm not sure why...

            - (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;

}

江南烟雨〆相思醉 2024-08-22 08:59:32

您的文本字段未显示,因为您从未设置文本字段的框架。您需要将框架设置为正宽度和高度。我使用的高度是 43(行高 - 1px 灰线缓冲区)。

Your text field is not showing up because you never set the frame of the text field. You need to set the frame with a positive width and height. The height I would use is 43 (row height - 1px grey line buffer).

迷鸟归林 2024-08-22 08:59:32

啊哈!好的,我明白了!现在我想做的是通过查看单元格中左侧标题标签的宽度来动态定义详细信息文本字段的宽度。大多数时候我在模拟器中运行它,标题标签大小为 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];

Aha! OK, I got it! Now what I'm trying to do is dynamically define the width of the detail text field by looking at the width of the title label left in the cell. Most of the times I run this in the simulator the title label size is 0, but occasionally I get a result. This doesn't make any sense to me, I'm thinking maybe it's because cells are being reused or so? Do you guys have any ideas?

I added this piece of code after textLabel is set:

CGSize textLabelSize = [textLabel.text sizeWithFont:textLabel.font];

And then in the case block at the beginning I do:

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文