我有UIViewController
和UITableViewCell
以及UITextField
和UIDatePicker
。如果更改第一个单元格UITextField
中的值,则更改单元格1和7中的值。如果更改第二个单元格UITextField
中的值,则更改单元格2和8中的值。有时从第一个单元格滚动值之后一秒钟内移动。为什么会这样呢?对于单元格,我创建了一个单独的类UITextField
和UIDatePicker
。
我的UIViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.stepsController.timeadayVal integerValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
addCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.titleLabel.text = [NSString stringWithFormat:@"%i прием",indexPath.row+1];
return cell;
}
UITableViewCell.m
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 320, 20)];
_titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:17];
_titleLabel.textColor = [UIColor darkGrayColor];
[self addSubview:_titleLabel];
_textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 35, 280, 35)];
UIView *paddingView6 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
_textField.leftView = paddingView6;
_textField.leftViewMode = UITextFieldViewModeAlways;
[_textField.layer setBackgroundColor: [[UIColor colorWithRed:249.0/255.0 green:249.0/255.0 blue:249.0/255.0 alpha:1] CGColor]];
[_textField.layer setBorderColor: [[UIColor colorWithRed:245.0/255.0 green:246.0/255.0 blue:247.0/255.0 alpha:1] CGColor]];
[_textField.layer setBorderWidth: 0.2];
[_textField.layer setCornerRadius:3.0f];
[_textField.layer setMasksToBounds:YES];
_textField.text = [NSString stringWithFormat:@"00:00"];
_textField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:16.0];
_textField.textColor = [UIColor grayColor];
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[itemsArray addObject:flexButton];
UIBarButtonItem *doneButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(inputAccessoryViewDidFinish:)];
[doneButton setTitleTextAttributes:@{
UITextAttributeFont: [UIFont fontWithName:@"HelveticaNeue-Light" size:15.0],
UITextAttributeTextColor: [UIColor colorWithRed:60.0/255.0 green:162.0/255.0 blue:161.0/255.0 alpha:1]
} forState:UIControlStateNormal];
[itemsArray addObject:doneButton];
[myToolbar setItems:itemsArray animated:NO];
UIDatePicker *timePicker = [[UIDatePicker alloc] init];
timePicker.datePickerMode = UIDatePickerModeTime;
[timePicker addTarget:self action:@selector(timeChanged:)
forControlEvents:UIControlEventValueChanged];
_textField.inputAccessoryView = myToolbar;
_textField.inputView = timePicker;
_textField.inputView = timePicker;
[self addSubview:_textField];
}
return self;
}
-(void)timeChanged:(id)sender{
UIDatePicker *picker=(UIDatePicker*)_textField.inputView;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"HH:mm"];
NSString *dateString = [dateFormat stringFromDate:picker.date];
_textField.text = dateString;
}
-(void)inputAccessoryViewDidFinish:(id)sender {
[_textField resignFirstResponder];
}
最佳答案
其次,带有文本字段显示错误的东西是结构问题。检查您的结构,并记住,每次滚动时,单元格都会加载新的。如果您一直添加子视图,那么就像是同一文本字段的4个视图之类的混乱情况,等等……请确保始终只有一个。
关于ios - UITableView + UITextField + UIDatePicker,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20562013/