PRADIP KUMAR PRADIP KUMAR - 6 months ago 30
iOS Question

My table view cell data is changing while scrolling ,why?

Anyone tell me why all times data changed? And what is the solution?

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

if(cell==nil)
{
NSArray *friendDetailObjects = [[NSBundle mainBundle] loadNibNamed:@"OrderNowCell" owner:self options:nil];

NSLog(@"%@", friendDetailObjects);

// cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell];
// cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell = [friendDetailObjects objectAtIndex:0];

cell.backgroundColor=[UIColor clearColor];
...

Answer

OK, you are going about this in entirely the wrong way.

First you should register the nib with the tableview...

[self.tableview registerNib:[UINib nibWithName:@"OrderNowCell" bundle:nil] forCellReuseIdentifier:@"OrderNowCell:]; 

Do this in viewDidLoad or something.

Then in cell for row...

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

    // this may need to be your custom cell subclass...
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderNowCell" forIndexPath:indexPath];

    cell.backgroundColor=[UIColor clearColor];

    // add other details to cell here.

    return cell;
}
Comments