UITextField在UITableViewCell帮助中 [英] UITextField in UITableViewCell Help

查看:114
本文介绍了UITextField在UITableViewCell帮助中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在互联网上搜索了一个很好的教程,或者发布了关于在每个单元格中填入UITextField以进行数据输入的UITableView。

I have scoured the internet looking for a good tutorial or posting about having a UITableView populated with a UITextField in each cell for data entry.

我想跟踪每个UITextField以及滚动时在其中写入的文本。 tableView将被分区。我一直在使用自定义UITableViewCell,但我对任何方法都开放。

I want to keep track of each UITextField and the text written within it while scrolling. The tableView will be sectioned. I have been using a custom UITableViewCell but I'm open to any method.

另外,是否可以将textFields用作ivars?

Also, is it possible to use the textFields as ivars?

如果你们中的任何一个人能指出我正确的方向,我们将不胜感激。

If any of you could point me in the right direction, it would be greatly appreciated.

提前谢谢!

推荐答案

要解决您的问题,您必须维护一个数组,其中包含一些数字(您添加到所有单元格的textFields数量)。

To solve your problem you have to maintain an array, with some number(number of textFields you added to all cells) of objects.

创建该数组时,需要将空NSString对象添加到该数组。每次加载单元格时,您必须将受尊重的对象替换为受尊重的textField。

While creating that array you need add empty NSString objects to that array. and each time while loading the cell you have to replace the respected object to respected 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];
}

问候,

萨蒂亚

这篇关于UITextField在UITableViewCell帮助中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆