W D*_*son 11 iphone uitableview uitextfield ipad ios
我已经浏览了互联网,寻找一个很好的教程或发布关于在每个单元格中填充UITextField以进行数据输入的UITableView.
我想跟踪每个UITextField以及滚动时写在其中的文本.tableView将被分区.我一直在使用自定义UITableViewCell,但我对任何方法都开放.
另外,是否可以将textFields用作ivars?
如果你们中的任何一个人能指出我正确的方向,我将不胜感激.
先感谢您!
Sat*_*tya 10
要解决您的问题,您必须维护一个数组,其中包含一些数字(您添加到所有单元格的textFields数量)的对象.
在创建该数组时,您需要将空NSString对象添加到该数组.每次加载单元格时,您必须将受尊重的对象替换为受尊重的textField.
请检查以下代码.
- (void)viewDidLoad{
textFieldValuesArray = [[NSMutableArray alloc]init];
for(int i=0; i<numberofRows*numberofSections; i++){
[textFieldValuesArray addObject:@""];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return numberofSections;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return numberofRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
CustomTextField *tf = [[CustomTextField alloc] initWithFrame:CGRectMake(5,5,290,34)];
tf.tag = 1;
[cell.contentView addSubView:tf];
[tf release];
}
CustomTextField *tf = (CustomTextField*)[cell viewWithTag:1];
tf.index = numberofSections*indexPath.section+indexPath.row;
tf.text = [textFieldValuesArray objectAtIndex:tf.index];
return cell;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
int index = textField.index;
[textFieldValuesArray replaceObjectAtIndex:index withObject:textField.text];
}
Run Code Online (Sandbox Code Playgroud)
问候,
萨蒂亚
首先,您必须了解UITableViewCell和UITextField只是视图,它们不应该保存数据,它们只是显示它们并允许用户与它们交互:数据应该保存在表的控制器中视图.
您必须记住,UITableView允许您重用UITableViewCell实例以达到性能目的:屏幕上显示的内容实际上是UITableView保留的唯一子视图.这意味着您将重用一个已包含文本字段的单元格,并直接在该字段上设置文本.当用户点击该字段时,它将对其进行编辑,并且当用户完成时您必须从中获取该值.
最快的方法是使用Satya提出的建议,即构建普通的UITableViewCell并插入UITextField(不需要CustomTextField类......).标签将允许您轻松返回文本字段...但是您必须设置文本字段,以便在表视图调整大小或同一单元格中的标签发生更改时其行为正常.
最简洁的方法是创建UITableViewCell的子类并设置标签和文本字段的布局,并且可以将文本字段作为自定义子类的属性提供.
| 归档时间: |
|
| 查看次数: |
10180 次 |
| 最近记录: |