UITableView部分在调用时不重新加载

时间:2012-04-21 16:26:09

标签: objective-c ios uitableview

我想在调用ViewWillAppear方法时重新加载我的表视图的一部分,我已经这样实现了:

- (void)viewWillAppear:(BOOL)animated {

    NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:0 inSection:1];
    reloadRows = [NSArray arrayWithObjects:rowToReload, nil];
    [self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
}

这是rowforsection方法,它指示哪些内容应出现在每个tableview部分中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"fadk");
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"PINGAS"];
    [self.tableView setAlwaysBounceVertical:YES];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                       reuseIdentifier:@"PINGAS"] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;


       // if ([indexPath section] == 0) {
            UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 3, 300, 41)];
            UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 0, 300, 120)];
            UIView *paddingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 0)] autorelease];
            paddingView.backgroundColor = [UIColor cyanColor];


          //  if ([indexPath row] == 0) {
        if ([indexPath section] == 0) {
            NSLog(@"0");
            [cell addSubview:textField];
            if ([indexPath row] == 0) {
                textField.placeholder = @"Title";
            }
            else{
                textField.placeholder = @"Location";
            }
        }
        else if ([indexPath section] == 1) {
            NSLog(@"1");
            NSDateFormatter *formatter;
            NSString *eSString1;
            NSString *eEString2;
            formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"h:mm a"];

            cell.textLabel.text = @"Starts\nEnds";
            cell.textLabel.numberOfLines = 2;
            eSString1 = [formatter stringFromDate:eSTime];
            eEString2 = [formatter stringFromDate:eEtime];
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@\n%@", eSString1, eEString2];
            cell.detailTextLabel.numberOfLines = 2;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        }
        else{

            NSLog(@"2");
            [cell addSubview:textView];

        }

            textField.delegate = self;
            textField.leftView = paddingView;
            textField.leftViewMode = UITextFieldViewModeAlways;
            textField.adjustsFontSizeToFitWidth = YES;
            textField.textColor = [UIColor blackColor];
            textField.keyboardType = UIKeyboardTypeAlphabet;
            textField.returnKeyType = UIReturnKeyDone;  
            textField.backgroundColor = [UIColor clearColor];
            textField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
            textField.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
            textField.textAlignment = UITextAlignmentLeft;
            textField.tag = 0;
            //playerTextField.delegate = self;

            textField.clearButtonMode = UITextFieldViewModeNever; // no clear 'x' button to the right
            [textField setEnabled: YES];



            [textField release];

        textView.delegate = self;
        textView.textColor = [UIColor blackColor];
        textView.keyboardType = UIKeyboardTypeAlphabet;
        textView.returnKeyType = UIReturnKeyDone;
        textView.backgroundColor = [UIColor clearColor];
        textView.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
        textView.autocapitalizationType = UITextAutocapitalizationTypeNone; // no auto capitalization support
        textView.textAlignment = UITextAlignmentLeft;
        textView.tag = 0;



        [textView release];
       // }
    }
    return cell;    
}

第一次调用viewWillAppear时,这是第一次加载,但在此之后,该部分似乎从第一次加载和第二次加载中回收数据,并且当它仍然进入cellforrow部分时,它不再进入我在viewWIllAppear部分调用的部分。

1 个答案:

答案 0 :(得分:7)

重新加载应该夹在开始/结束更新之间:

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];