Jerome Jerome - 3 months ago 12
Objective-C Question

UITableView section not reloading when called

I want a section of my table view to reload whenever the ViewWillAppear method is called, I've implemented this like so:

- (void)viewWillAppear:(BOOL)animated {

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


Here is the rowforsection method that indicates which content should appear in each tableview section:

- (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;
}


This works swimmingly the first load, and I after the first calling of viewWillAppear, but after that the section seems to recycle the data from the first load and the second load, and while it still enters the cellforrow section, it no longer goes into the section I call in the viewWIllAppear section.

Answer

The reload should be sandwiched between begin / end updates:

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