1

我希望每当调用 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 部分调用的部分。

4

1 回答 1

7

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

[self.tableView beginUpdates];
[self.tableView reloadRowsAtIndexPaths:reloadRows withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
于 2012-04-21T16:48:23.430 回答