0
votes

in the ui tableview cell, the statement if (cell == nil) is not called.

Because when i initialize the cell outside the if statement, the talbe will have some wrong label in wrong row.

how can I initialize my cell?

The @"tttestttt" NS LOG is not all the time when the table is created.

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

static NSString *MyIdentifier = @"GroupsCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    NSLog(@"tttesttttt");
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:MyIdentifier];
}

}

3

3 Answers

0
votes

dequeueReusableCellWithIdentifier: always return a cell if you're using Storyboard.

1
votes

Use the more convenient method dequeueReusableCellWithIdentifier:forIndexPath: which returns always a valid cell.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
    static NSString *MyIdentifier = @"GroupsCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier 
                                                        forIndexPath:indexPath];

    ...

    return cell;
}
0
votes

make Identifier not a fixed value

make it as index.row (convert to string as identity)

your wrong may be crate by cell refuse