1

I am doing using some code that I have seen work before. Essentially a user answers yes or no on a post with some buttons. Pressing yes or no updates the database, which is working correctly, and it also updates the visible UI, which is not working. This UI updates the buttons so they one is selected, other is highlighted and both are disabled for user interaction. Also it makes changes to two UILabels. The method that these buttons calls needs to update the database and retrieve the buttons from the tableViewCell and update the changes I have the methods working in another ViewController so I can not understand the difference here. Here is my cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *simpleTableIdentifier = [NSString stringWithFormat:@"%ld,%ld",(long)indexPath.section,(long)indexPath.row];
NSLog(@" simple: %@",simpleTableIdentifier);
if (indexPath.row==0) {
    ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:simpleTableIdentifier];
    }


    cell = [self createProfileCell:cell];
    return cell;

}else{
    YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell==nil) {
        cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell = [self createYesNoCell:cell:indexPath];
    return cell;

  }
}

Essentially what this does is create the users profile in the first cell, and load all the questions that user asks. This is the major difference I see between the old tableView and this tableView. In createYesNoCell I create the UIElements and create tags as follows

    cell.yesVoteButton.tag=indexPath.row+ yesVoteButtonTag1;
    cell.noVoteButton.tag=indexPath.row+ noVoteButtonTag1;
    cell.yesCountLabel.tag=indexPath.row+ yesCountLabelTag1;
    cell.noCountLabel.tag=indexPath.row+ noCountLabelTag1;

The buttons have the selector that initiates a number of things. It finds which button was pressed by the following.

     NSInteger index;
if(sender.tag>=yesVoteButtonTag1){
    NSLog(@"Yes button pressed");
    votedYes=true;
    index=sender.tag-yesVoteButtonTag1;

}else{
    NSLog(@"No button Pressed");
    votedYes=false;
    index=sender.tag-noVoteButtonTag1;
}

     UILabel *yesLabel = (UILabel*) [self.tableView   viewWithTag:index+yesCountLabelTag1]; // you get your label reference here
     UIButton *yesButton=(UIButton *)[self.tableView viewWithTag:index+1+yesVoteButtonTag1];
     NSLog(@"Tag IN METHOD: %ld",index+yesVoteButtonTag1);
     UILabel *noLabel = (UILabel*) [self.tableView viewWithTag:index+1+noCountLabelTag1]; // you get your label reference here
     UIButton *noButton=(UIButton *)[self.tableView viewWithTag:index+noVoteButtonTag1];

These viewWithTag calls are nil when I look at them. The only difference that I can see from my earlier implementation is that the old one had sections and one row, while this one is all rows and one section. So replacing the indexPath.section with indexPath.row should account for that. Also I checked that the tag made in cellForRowAtIndexPath is the same as the row recovered in the yes/no vote method, because it is displaced by one because of the profile cell being created at indexPath.row==0. I tried passing the cell to the yes/no vote method and tried to recover the buttons and labels with contentView as some suggestions made on similar posts. However this didn't seem to solve my problem. Really would appreciate some insight on this.

Kyle Griffith
  • 607
  • 1
  • 6
  • 12

3 Answers3

0

have you call the '[tableView reload]' method to update the UITableView, it may helps.

Nan
  • 496
  • 3
  • 21
0

Firstly, the table reuse identifier should be used for types of cells, not one for each cell. You have two types, so you should use two fixed reuse identifiers.

ProfileFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ProfileCell"];

if (cell == nil) {
    cell = [[ProfileFirstCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:@"ProfileCell"];
}

and

YesNoCell *cell =[self.tableView dequeueReusableCellWithIdentifier:@"YesNoCell"];
if (cell==nil) {
    cell=[[YesNoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YesNoCell"];
}

Secondly, rather than trying to get a reference to a cell after creating the table, which isn't working for you, you should initialize the cells completely when they are created. (TableView won't create cells unless they're visible, so you shouldn't rely on their existing at all.)

createProfileCell should really be called initializeProfileCell, because you're not creating the cell in it - you already did that in the line above, or recovered an old one.

Then your call to initializeProfileCell can take a flag specifying whether it is a Yes or No cell and set its properties accordingly.

cell = [self initializeProfileCell:cell isYes:(indexPath.section==0)];

Similarly with createYesNoCell --> initializeYesNoCell.

frankleonrose
  • 355
  • 2
  • 11
0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    static NSString *CellIdentifier = @"YOURCELL_IDENTIFIER";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
   UILabel *title = (UILabel*) [cell viewWithTag:5];
   UILabel *vensu =(UILabel*) [cell viewWithTag:7];
   vensu.text = @"YOUR TEXT";

   title.text = @"YOUR TEXT";

   return cell;
}
Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49