我是 iOS 开发新手。我在 Xcode 6( Storyboard)中创建我的应用程序。
我的问题是文本输入文本字段,滚动 TableView 随机将文本字段文本随机播放到其他文本字段。我在这样的堆栈溢出中看到了两个问题。但那个答案并不能解决问题。
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
在 Main.storyboard 中添加 UITableView 并连接 IBOutlet 并添加数据源和委托(delegate)。
//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
请详细解释一下。提前致谢
最佳答案
我找到了答案......只需通过标签文本将文本字段文本放入字典 setObject 中,然后再次通过标签文本检查将文本分配给相应的文本字段......这是我的代码......
//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];
}
滚动表格 View 时没有错误...
关于ios - 自定义 UITableViewCell 中的 UITextFields 文本在滚动时出现在其他单元格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27875329/