自定义 UITableViewCell 中的 UITextFields 文本在滚动时显示在其他单元格中



我是iOS开发的新手。 我在 Xcode 6(情节提要)中创建我的应用程序。

我的问题是文本输入文本字段,

滚动表格视图,将文本字段文本随机打乱到其他文本字段。我在这样的堆栈溢出中看到两个问题。但这个答案并不能解决问题。

  1. MoneyEntry.xib 是一个具有一个标签和一个文本框的 uitableviewcell。并向其添加类文件并连接IBOutlet并将标识符作为"MoneyEntryIdentifier"添加到uitableviewcell。

    //MoneyEntryTableViewCell.h
    @interface MoneyEntryTableViewCell : UITableViewCell
    //Money Entry Cell
    @property (strong, nonatomic) IBOutlet UILabel *lblMemName;
    @property (strong, nonatomic) IBOutlet UITextField *textAmount;
    @end
    
    //MoneyEntryTableViewCell.m
    #import "MoneyEntryTableViewCell.h"
    @implementation MoneyEntryTableViewCell
    @synthesize lblMemName,textAmount;
    - (void)awakeFromNib {
    }
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    }
    @end
    
  2. 在 Main.storyboard 中添加 UITableView 并连接 IBOutlet,并添加数据源和委托。

    //MoneyDetailViewController.h
    #import <UIKit/UIKit.h>
    @interface MoneyDetailViewController : UIViewController
    @property (strong, nonatomic) IBOutlet UITableView *tblVwMoneyEntry;
    @end
    
    //MoneyDetailViewController.m
     @interface MoneyDetailViewController ()
     {
       NSArray *tabledata;
     }
     @end
     @implementation MoneyDetailViewController
     - (void)viewDidLoad {
      [super viewDidLoad];
      tabledata = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto",@"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee",@"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", nil];
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
     return [tabledata count];
    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *CellIdentifier = @"MoneyEntryIdentifier";
     static NSString *CellNib = @"MoneyEntry";
     MoneyEntryTableViewCell *cell = (MoneyEntryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil)
     {
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
      cell = (MoneyEntryTableViewCell *)[nib objectAtIndex:0];
     }
     UILabel     *lblname  = (UILabel *)    [cell lblMemName];
     UITextField *txtfield = (UITextField *)[cell textAmount];
     txtfield.tag = indexPath.row;
     lblname.text = [tabledata objectAtIndex:indexPath.row];
     txtfield.placeholder =@"0.00";
     cell.selectionStyle =UITableViewCellSelectionStyleNone;
     return cell;
     }
    @end
    

请详细解释一下。提前致谢

我找到了答案....只需将文本字段文本放入字典设置对象按标签文本,然后再次检查按标签文本将文本分配给相应的文本字段。这是我的代码...

//In Interface
NSMutableDictionary *amounts;
amounts =[[NSMutableDictionary alloc]init];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"MoneyEntryIdentifier";
static NSString *CellNib = @"MoneyEntry";
MoneyEntryTableViewCell *cell = (MoneyEntryTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
    cell = (MoneyEntryTableViewCell *)[nib objectAtIndex:0];
}
UILabel     *lblname  = (UILabel *)    [cell lblMemName];
lblname.tag =100;
UITextField *txtfield = (UITextField *)[cell textAmount];
txtfield.tag =indexPath.row;
[txtfield addTarget:self  action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
lblname.text = tabledata[indexPath.row];
txtfield.placeholder = [NSString stringWithFormat:@"%ld",(long)indexPath.row];

if ([amounts valueForKey:lblname.text] != nil) {
    txtfield.text = [amounts valueForKey:lblname.text];
} else {
    txtfield.text = @"";
}
cell.selectionStyle =UITableViewCellSelectionStyleNone;
return cell;
}
 -(void)textFieldDidChange:(UITextField *)txtField
{
UILabel *label = (UILabel *)[txtField.superview viewWithTag:100];
NSString *labelString = label.text;
NSString *textFieldString = txtField.text;
[amounts setObject:textFieldString forKey:labelString];
}

滚动表视图时没有错误...

您是否正在执行某些操作来存储输入到文本字段中的文本。该UITableView通过重用不可见或从屏幕中消失的UITableViewCells来节省内存。

因此,如果您在单元格的文本字段中输入一些文本,并且该单元格退出屏幕,则该单元格将排队供以后使用。滚动时,将从此队列中添加新行,从而恢复带有先前文本的textfield。这就是为什么您会看到文本在任何地方随机弹出的原因。

您需要将每个textfield的数据保存在单独的数组中,并在cellForRowAtIndexPath:函数中重置它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Your code..
    Item *item = itemArray[indexPath.row]
    cell.txtfield.text = item.amount
}

可以使用 UITextField's UIControlEventEditingChanged 事件来存储更改的数组值。

-(void)textFieldDidChange:(UITextField *)txtField
{
    Item *item = itemArray[txtField.tag] 
    item.amount = txtField.text
}

添加文本字段更改方法,如下所示

[textField addTarget:self 
              action:@selector(textFieldDidChange:) 
    forControlEvents:UIControlEventEditingChanged];

使用名为 Item 的自定义类来存储数据。

class Item
{
     @property (nonatomic,strong) NSString *itemName;
     @property (nonatomic) float amount;  
}

itemArray将包含您的项目。

Item *it1 = [[Item alloc]init]
it1.itemName = @"Eggs Benedict"
it1.amount = ""
.... 
itemArray = [NSArray arrayWithObjects:it1,it2,it3,nil];

最新更新