开发者

Having trouble with UITableViewCell loaded from nib

I am having trouble wiht UITableViewCell loaded from nib. I used apple documentation to that but I do't know where I am going wrong. UITableViewCell created through IB contains 5 UIlable. Nib are loaded perfectly but the problem is when I scroll text of the label changes automatically to different. Following is the my code for cell for row at indexpath. Please let me know where I am going worng.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowNo = [indexPath row];
NSLog(@"Row No:%d",rowNo);
Transfer *tempTransferRecord =(Transfer*)[self.transferInformation objectAtIndex:rowNo];
seasonYear.text=tempTransferRecord.seasonYear;
longName.text=tempTransferRecord.longName;
transactionDate.text=tempTransferRecord.transactionDate;
toTeam.text=tempTransferRecord.toTeam;
fromTeam.text=tempTransferRecord.f开发者_Go百科romTeam;
/*
static NSString *MyIdentifier = @"MyIdentifier2";    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier] ;

if (cell == nil) 
{
    [[NSBundle mainBundle] loadNibNamed:@"PlayerTransfers" owner:self options:nil];
    cell=transfersInfoCell;
    self.transfersInfoCell=nil;
}

 */


static NSString *CellIdentifier = @"MyIdentifier2";
static NSString *CellNib = @"PlayerTransfers";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CellNib owner:self options:nil];
    cell = (UITableViewCell *)[nib objectAtIndex:0];
}

// perform additional custom work...

return cell;

}


Try moving your cell loading code to the top of cellForRowAtIndexPath, and the rowNo and value setting after that. Cells changing content must mean you're either changing what's in the data source between when you first set it and when you scroll, or you're setting values at the wrong point in time (i.e setting values on row 5 but then loading row 6 from a nib, or setting values on a row but then loading the nib for it afterwards so the values you set are overridden). Also, I'm a bit concerned that you set local variables rather than, say, (PlayerTransfers*)cell.seasonYear.text = tempTransferRecord.seasonYear or (UILabel *)[cell viewWithTag:1].text = tempTransferRecord.seasonYear. Shouldn't you be setting the values within each cell rather than on your table view class? That could explain why values are changing to something different: you may use the same variables to set every cell.


Have you set a CellIdentifier in Interface Builder?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜