我需要創建應用程序內使用的自定義設置視圖。該應用程序是一個模型,所以它實際上已經有一個,它使用UITableViewCellStyleValue1,但沒有任何可編輯的。所以基本上我想要的是同樣的事情,但是可編輯的detailTextLabel(右側的標籤)是什麼,最好的方法是什麼?如何創建自定義設置視圖/使單元格標籤可編輯
0
A
回答
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;
相關問題
- 1. 如何設置表格視圖單元格以適應標籤?
- 2. 如何使用自定義視單元格創建NSTableview
- 3. 使用自定義可編輯單元格創建JFX TableView的問題
- 4. 如何創建下拉表格視圖使用兩個自定義單元格?
- 5. 如何創建自定義編輯視圖MVC?
- 6. 創建自定義單元格時寫入標籤Xcode
- 7. 使用自定義單元格/圖像和標籤的表格視圖
- 8. 自定義單元格的設置標籤
- 9. 如何創建自定義單元格與目標C標籤和文本框
- 10. TableView,設置可編輯的單元格
- 11. 單元格的自定義編輯
- 12. 如何在表格視圖單元格上創建自定義按鈕
- 13. didSelectRowAtIndexPath爲自定義標籤單元格
- 14. 自定義單元格標籤
- 15. 如何在編輯UIImagePickerController的視圖上設置自定義覆蓋視圖?
- 16. 如何創建自定義javadoc標籤?
- 17. 如何讓NSTableView使用與預設單元格視圖混合的自定義單元格視圖?
- 18. 如何從子視圖自定義單元格Refersh UI視圖?
- 19. 無法在編程創建的自定義單元格中設置透明度
- 20. 創建自定義標籤
- 21. 創建自定義標籤
- 22. 如何編輯自定義可可插件的子視圖
- 23. 如何使用自定義文本放置創建基於照片的表格視圖單元格?
- 24. LWUIT標籤:創建自定義標籤
- 25. 創建自定義單元格
- 26. 使用R中的自定義標籤創建傳單圖
- 27. 如何以編程方式創建訂單時設置自定義價格?
- 28. iPhone:UITableView與自定義單元格設置
- 29. 如何從視圖控制器設置數據UITableView的自定義單元格
- 30. 自定義單元格的表視圖