objective-c - 调用时 UITableView 部分不重新加载

标签 objective-c ios uitableview

我希望在调用 ViewWillAppear 方法时重新加载我的 TableView 的一部分,我已经这样实现了:

- (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 部分调用的部分。

最佳答案

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

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

关于objective-c - 调用时 UITableView 部分不重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10260834/

相关文章:

ios - 可调整大小的图像

iphone - NSPersistentStoreCoordinator 出现 nil 模型错误?

iphone - NSInputStream 的例子?

iphone - iPhone 旋转时更改单元格中的背景图像

ios - 在 numberOfSectionsInTableView 中使用 NSFetchedResultsController 对日期进行分组

ios - 如何增加角标(Badge)数量

ios - 无法在 iphone 上获得正确的频率值

ios - Swift 重载问题

objective-c - NSLog 在设备上被忽略

iphone - 加载 UIView 到 navigationalStack 不传递字符串到新 View