Why is my UITableViewCell not deselecting or updating its text?

I have a UITableView with a list of stories and a cell at the bottom that loads more stories. I am trying to make the "Additional Stories ..." cell deselect and change its text to "Loading ..." when clicked. I have searched all over the Internet and all over the stack stream, and I cannot understand why my code is not working correctly. Right now, when the "Advanced Stories ..." cell is clicked, it remains selected and never changes its text.

For those who ask, moreStories adds 25 new stories at the bottom of the table.

Original code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
if (storyIndex == [stories count]) {
    UITableViewCell *moreCell = [tableView dequeueReusableCellWithIdentifier:@"more"];
    if (moreCell == nil) {
        moreCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"more"] autorelease];
    } // Set up the cell
    moreCell.textLabel.text = @"Loading...";

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self moreStories];
} else { 
    NSLog(@"%@",[[stories objectAtIndex: storyIndex] objectForKey: @"link"]);
    webViewController *webController;
    webController = [[webViewController alloc] initWithURLPassed:[[stories objectAtIndex: storyIndex] objectForKey: @"link"]];
    [self.navigationController pushViewController:webController animated:YES];
    [webController release];
    webController =nil;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
}
}

, . "...", , moreStories . , , "Loading...", 1/4/10 ~ 9

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
if (storyIndex == [stories count]) {
    UITableViewCell *moreCell = [tableView cellForRowAtIndexPath:indexPath];
    moreCell.textLabel.text = @"Loading...";
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSArray *arr = [[NSArray alloc] initWithObjects:indexPath,nil];
    [tableView beginUpdates];
    [tableView reloadRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade];
    [tableView endUpdates];
    [self moreStories];
} else { 
    NSLog(@"%@",[[stories objectAtIndex: storyIndex] objectForKey: @"link"]);
    webViewController *webController;
    webController = [[webViewController alloc] initWithURLPassed:[[stories objectAtIndex: storyIndex] objectForKey: @"link"]];
    [self.navigationController pushViewController:webController animated:YES];
    [webController release];
    webController =nil;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
}
}
+2
2

[tableView dequeueReusableCellWithIdentifier:@"more"] , , , . , moreCell == nil , . .

[tableView cellForRowAtIndexPath:indexPath], .

UITableViewCell *moreCell = [tableView cellForRowAtIndexPath:indexPath];
moreCell.textLabel.text = @"Loading...";

: , , , aBitObvious , URL- . , .

+2

, . , . , UITableView. , . , , tableView.

- (void)viewDidLoad {

  NSArray *array = [[NSArray alloc] initWithObjects:@"Story 1", @"Story 2", @"Story 3", @"Story 4", @"Story 5", @"More Stories", nil];
  self.stories = array;
  [array release];

  [super viewDidLoad];
}

//TableView DataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
 {
   return [stories count];
 }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

  static NSString *CellIdentifier = @"CellIdentifier";
  UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier]; 
  if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: CellIdentifier] autorelease];
  }

  cell.textLabel.text = [stories objectAtIndex:[indexPath row]];
  return cell;
 }

// TableView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if ([indexPath row] == [stories count] - 1) {
    //Get more stories
    [table deselectRowAtIndexPath:indexPath animated:YES];

    NSArray *array = [[NSArray alloc] initWithObjects:@"Story 1*", @"Story 2*", @"Story 3*", @"Story 4*", @"Story 5*", @"Loading...", nil];
    self.stories = array;
    [array release];

    NSArray *arr = [[NSArray alloc] initWithObjects:indexPath,nil];

    [table beginUpdates];
    [table reloadRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade];
    [table endUpdates];
}
else {
    //Call your methods to load the stories.
}
0

All Articles