2009-12-21 84 views
0

我需要创建应用程序内使用的自定义设置视图。该应用程序是一个模型,所以它实际上已经有一个,它使用UITableViewCellStyleValue1,但没有任何可编辑的。所以基本上我想要的是同样的事情,但是可编辑的detailTextLabel(右侧的标签)是什么,最好的方法是什么?如何创建自定义设置视图/使单元格标签可编辑

回答

2

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

您还可以通过该标签相匹配的文本的颜色:

yourTextField.textColor = cell.detailTextLabel.textColor; 

您可能需要与UITextField实例的属性框摆弄,以获得对准恰到好处。

0

好了,这里是我现在,不显示文本字段代码,我不知道为什么...

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

}

0

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

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]; 
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; 
相关问题