iPhone:如何构建类似于联系人地址单元格的自定义表视图单元格
这可能已经讨论过了,但我找不到问题所在 我需要在表视图中显示/编辑地址。查看iPhone联系人应用程序中的地址单元格,他们似乎使用一个单元格来显示和编辑地址 我最初认为这是通过使用4个自定义单元格来编辑地址来完成的,但我看不出地址类型字段如何跨越4行,此外,选中时,它选择了所有4行。这让我觉得它是用一个表视图单元格完成的,它有一个自定义的UItextFields排列 目前我正沿着这条路走。目前让我陷入困境的是处理细胞之间的灰线。我最初打开了UItextField边框,但看起来不是很好。所以我正在考虑做自定义绘图。但这似乎并没有在细胞顶部绘制 有人知道苹果是如何构建这个地址单元的吗iPhone:如何构建类似于联系人地址单元格的自定义表视图单元格,iphone,uitableview,street-address,Iphone,Uitableview,Street Address,这可能已经讨论过了,但我找不到问题所在 我需要在表视图中显示/编辑地址。查看iPhone联系人应用程序中的地址单元格,他们似乎使用一个单元格来显示和编辑地址 我最初认为这是通过使用4个自定义单元格来编辑地址来完成的,但我看不出地址类型字段如何跨越4行,此外,选中时,它选择了所有4行。这让我觉得它是用一个表视图单元格完成的,它有一个自定义的UItextFields排列 目前我正沿着这条路走。目前让我陷入困境的是处理细胞之间的灰线。我最初打开了UItextField边框,但看起来不是很好。所以我正在
谢谢我有些东西在工作,但我不确定它是否建得那么好。我基本上创建了一个UIView,它为地址的各个字段保存UITextFields。我为它构建了一个自定义控制器,它管理数据并在字段之间绘制灰线,就像contacts中的一样 然后,我将此自定义UIView添加到UITableView单元格中。我的理解是,这是推荐的方法,因为drawRect:中的任何图形都位于UITableViewCell的边界内。我使用drawRect的实验:在UITableView中,单元格类在UITableView的背景上绘制线条,而不是单元格 这基本上是可行的,但是有一件事我不喜欢。为了使灰线延伸到UITableViewCell的边界,我必须确保放置在其中的UIView的大小完全相同,以便在drawRect:中完成的任何绘图都可以直接延伸到UITableViewCell的边缘
但是,如果我使用不透明的背景,我会松开单元格的漂亮圆角,因为它是在顶部绘制的。所以我必须使用透明的背景。我的理解是,出于性能原因,建议使用不透明背景,因此我不确定是否真的正确构建了此控件。幸运的是,屏幕上不会有超过2或3个单元格。如果希望在表格视图中的每个单元格之间有一条线,请使用“分组”样式-请参阅Interface Builder中属性检查器中的“表格视图”选项卡。每个小组的成员之间都有一条线
您是否考虑使用UITabLIEVIEW单元格而不是以编程方式添加UIVIEW?您可以轻松地向这样的XIB添加标签和文本字段+垂直线。使用以下代码,这对我来说非常有效:
@protocol PersonDetailCellViewControllerDelegate
-(void)didUpdateTextField:(int)index withText:(NSString*)text;
@end
@interface PersonDetailCellViewController : UITableViewCell <UITextFieldDelegate> {
IBOutlet UILabel* keyLabel;
IBOutlet UITextField* valueTextField;
int index;
id<PersonDetailCellViewControllerDelegate> delegate;
}
@property (nonatomic, retain) IBOutlet UILabel* keyLabel;
@property (nonatomic, retain) IBOutlet UITextField* valueTextField;
@property (nonatomic, retain) id<PersonDetailCellViewControllerDelegate> delegate;
@property (nonatomic, assign) int index;
@end
使用相应的UITableView方法
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PersonDetailCellViewController *cell = (PersonDetailCellViewController*)[aTableView dequeueReusableCellWithIdentifier:CELL_KEY_VALUE_ID];
if(cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"PersonDetailCellView" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (PersonDetailCellViewController*)currentObject;
cell.valueTextField.backgroundColor = [UIColor clearColor];
cell.valueTextField.borderStyle = UITextBorderStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
if([Language sharedLanguage].rightToLeft) {
// set the alignment right because we are flipping the whole available text field, not just the area which actually has text
cell.keyLabel.textAlignment = UITextAlignmentRight;
cell.valueTextField.textAlignment = UITextAlignmentLeft;
// flip cell contents
cell.contentView.layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
// flip text back so that readable
cell.keyLabel.layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
cell.valueTextField.layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
break;
}
}
}
cell.keyLabel.text = [keys objectAtIndex:indexPath.section];
cell.valueTextField.placeholder = [hints objectAtIndex:indexPath.section];
cell.valueTextField.delegate = cell;
if(indexPath.section == 0) {
cell.valueTextField.text = self.name;
} else if(indexPath.section == 1) {
cell.valueTextField.text = self.mobile;
} else if(indexPath.section == 2) {
cell.valueTextField.text = self.home;
}
if(indexPath.section == 1) {
cell.valueTextField.keyboardType = UIKeyboardTypePhonePad;
} else {
cell.valueTextField.keyboardType = UIKeyboardTypeDefault;
}
// open keyboard
if(indexPath.section == 0) {
[cell.valueTextField becomeFirstResponder];
} else {
[cell.valueTextField resignFirstResponder];
}
cell.delegate = self;
cell.index = indexPath.section;
return cell;
}
向您致意,托马斯。请执行以下操作:
- 使用单元格的
样式UITableViewCellStyleValue2
- 将
的cell.detailsTextLabel
属性设置为您选择的数字numberOfLines
- 实现
方法,并返回使用以下公式计算的高度:-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(nsindepath*)indepath
44.0f+(numberOfLines-1)*19.0f
我在网上的某个地方读到了这篇文章,但不记得来源了。今天刚试用过,效果完美。谢谢托马斯。我已经在使用分组单元格了。我试图模仿通讯录中地址单元的工作方式。使用联系人地址表明正在使用单个单元格。另外,就像编辑联系人地址一样,我在每一行上使用了不止一个字段。谢谢。当我格式化地址的只读显示时,它将非常有用。正是编辑模式显示给了我这些问题:-)
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
PersonDetailCellViewController *cell = (PersonDetailCellViewController*)[aTableView dequeueReusableCellWithIdentifier:CELL_KEY_VALUE_ID];
if(cell == nil) {
NSArray* topLevelObjects = [[NSBundle mainBundle]loadNibNamed:@"PersonDetailCellView" owner:nil options:nil];
for(id currentObject in topLevelObjects) {
if([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (PersonDetailCellViewController*)currentObject;
cell.valueTextField.backgroundColor = [UIColor clearColor];
cell.valueTextField.borderStyle = UITextBorderStyleNone;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
if([Language sharedLanguage].rightToLeft) {
// set the alignment right because we are flipping the whole available text field, not just the area which actually has text
cell.keyLabel.textAlignment = UITextAlignmentRight;
cell.valueTextField.textAlignment = UITextAlignmentLeft;
// flip cell contents
cell.contentView.layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
// flip text back so that readable
cell.keyLabel.layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
cell.valueTextField.layer.transform = CATransform3DMakeRotation(M_PI, 0.0f, 1.0f, 0.0f);
}
cell.selectionStyle = UITableViewCellSelectionStyleGray;
break;
}
}
}
cell.keyLabel.text = [keys objectAtIndex:indexPath.section];
cell.valueTextField.placeholder = [hints objectAtIndex:indexPath.section];
cell.valueTextField.delegate = cell;
if(indexPath.section == 0) {
cell.valueTextField.text = self.name;
} else if(indexPath.section == 1) {
cell.valueTextField.text = self.mobile;
} else if(indexPath.section == 2) {
cell.valueTextField.text = self.home;
}
if(indexPath.section == 1) {
cell.valueTextField.keyboardType = UIKeyboardTypePhonePad;
} else {
cell.valueTextField.keyboardType = UIKeyboardTypeDefault;
}
// open keyboard
if(indexPath.section == 0) {
[cell.valueTextField becomeFirstResponder];
} else {
[cell.valueTextField resignFirstResponder];
}
cell.delegate = self;
cell.index = indexPath.section;
return cell;
}