开发者

iPhone: Problem with setting custom cell's accesory

I can not set the custom cells accessory view while the page loads for the first time, any ideas?

In DetailView customcell's class I have this:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if(self.selected)
       self.accessoryType=UITableViewCellAccessoryCheckmark;
    else
       self.accessoryType=UITableViewCellAccessoryNone;
}

And In tableview controller:

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

      DetailViewCell *cell = [DetailViewCell  cellForTableView:tableView];

      if(somelogichere){            
        [cell setSelected:YES];
      }
        return cell;
}

I debug and hit that line so logic is true, but 开发者_高级运维in debug I also see that after it sets the accesory then again it calls twice the selected method, which overrrides the accessory to none in the else inthe second call. setSelected causes the selected method to be called twice and overrides the setting on the second call cause somehow on second call self.selected returns false;

UPDATE: I solved the problem by creating a boolean cellSelected property in the custom cell class and changing and checking its status rather than setting and changing the selected property of the cell, this is also better cause I can support multi selection tableview's better in future.


Does it work the second time after the view has loaded?

It most probably has something to do with following line:

  if(somelogichere){            
    [cell setSelected:YES];
  }

What is somelogichere ?

Depending on somelogichere your selected property might not be true the first time you run your code:

if(self.selected)
   self.accessoryType=UITableViewCellAccessoryCheckmark;
else
   self.accessoryType=UITableViewCellAccessoryNone;

Make sure you set your cell's selected property in the beginning. Or change the code inside your setSelected.

HTH


You try this code its help u

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{
    [super setSelected:selected animated:animated];
    UIImage *selectionBackground = [UIImage imageNamed:@"list_BG.jpg"];
    UIImageView *iview=[[UIImageView alloc] initWithImage:selectionBackground];

    self.selectedBackgroundView=iview;  
}

OR

In cellForRowAtIndexPath method of tableView:

cell.accessoryType = UITableViewCellAccessoryCheckmark;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜