У меня есть UIViewController
и UITableViewCell
с UITextField
и UIDatePicker
. Если я изменил значение в первой ячейке UITextField
, я изменяю значение в ячейке 1 и 7. Если я изменяю значение во второй ячейке UITextField
, я изменяю значение в ячейке 2 и 8. И после значения прокрутки из первой ячейки иногда перемещается в секунду. Почему это происходит? Для ячейки я создал отдельный класс, который равен UITextField
и UIDatePicker
.UITableView + 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];
}
То же самое случается. – Alexey
@Alexey вы поместили весь код выше в обратном вызове cellForRowAtIndexPath, и у вас больше нет глобальной ссылки? –
если я приношу значение всех ячеек в nslog, то я имею в виду 0,1,2,3 ячейки - это нуль. Если я изменил значение в первой ячейке 0,1,2,3, то остался без изменений и 6 изменений ячейки. – Alexey