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

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

问题描述

我在互联网上搜索了一个很好的教程,或者发布了关于在每个单元格中填充 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];
}

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

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