簡體   English   中英

UITableView + UITextField + UIDatePicker

[英]UITableView + UITextField + UIDatePicker

我有UIViewControllerUITableViewCellUITextFieldUIDatePicker 如果更改第一個單元格UITextField的值,則更改單元格1和7中的值。如果更改第二個單元格UITextField的值,則更改單元格2和8中的值。有時從第一個單元格滾動值之后一秒鍾內移動。 為什么會這樣呢? 我為單元格創建了一個單獨的類,它是UITextFieldUIDatePicker

我的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個視圖,如此等等……請確保所有時間都只有一個。

這似乎是單元重用問題。 當您調用dequeueReusableCellWithIdentifier:您可能會得到一個已經被使用的單元格。 發生這種情況時,您需要清除所有舊信息。 當前,您僅設置標題標簽文本。 但是,您還需要配置文本字段和日期選擇器(設置為索引路徑的默認值或保存的值)。

請編寫一些代碼。 無論如何,我認為對您來說最佳實踐是創建UIViewController,然后使用uiview而不是uitableviewcell,然后在uiview中添加uitextfield和日期選擇器,因為單元格用於uitableview。 另外我不明白你有多少個單元,最好用單元而不是uiviewcontroller創建uitableview。 您在代碼中的錯誤,請發布更新。 查看標記以及如何遷移數據。 所以請更新。

好,首先,您在錯誤的上下文中混淆了一些表達式。 我想我了解您的問題。

這樣做的原因是,如果滾動並且單元格滾動到屏幕之外,然后向后滾動,它將再次加載單元格方法。 因此,您更改的值不會保存在此方法中,因此它將再次調用舊值。

也許您應該將更改后的值存儲在臨時變量中,並在調用單元格的方法中存儲if。

if(value != tmpValue && tmpValue != 0) 
{
   value = tmpValue;
}
addCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

此函數不能保證返回單元格,它會返回任何可重用的單元格,但是看起來您沒有在任何地方設置重用標識符。 如果使用情節提要板創建單元的布局,然后引用其cellIdentifer,則此調用將返回它。 但是,除此之外,您還必須創建單元並設置其標識符。 我可以想象因此而得到一些意想不到的邏輯。

也不要存儲對單元的全局引用,並檢查出隊函數是否返回nil。 像這樣:

static NSString *CellIdentifier = @"Cell";
UITableViewCell *addCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(addCell == nil)
{
     addCell = [[UITableViewCell alloc] initWithStyle: <style> reuseIdentifier:CellIdentifier ];
}

...
...

看起來/聽起來像您在多個位置使用同一單元實例。 當一個單元改變時,它們都將在刷新單元格時改變。 出隊功能的目的是基於已經編譯/創建的單元格對象返回一個新實例。 它們都必須是單獨的實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM