2009-12-21 53 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; 
相關問題