在我的表格中查看单元格大小适合整个屏幕大小。所以它只创建两个单元,但从第三个单元使用旧单元格的内容...所以我得到不正确的数据..uitableview加载单元格的内容
如何解决此问题...?
谢谢。
- (UITableViewCell *)tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell = [[[UITableViewCell alloc] initWithFrame:CellFrame reuseIdentifier:CellIdentifier] autorelease];
for(UIView *v in [cell.contentView subviews])
[v removeFromSuperview];
curCellNo = indexPath.row;
if(curCellNo == 0){
min = 0;
max = 8;
x=0;
y=yForFirst;
col = 1;
}else{
if(curCellNo < prevCellNo){
min = min-12;
max = max-12;
}else{
min = max;
max = min+12;
}
x=0;
y=0;
}
prevCellNo = curCellNo;
NSLog(@"Max...%d",max);
NSLog(@"Min...%d",min);
NSLog(@"songsCount...%d",[songs count]);
for(int i=min; i<max && i<[songs count]; i++){
Song *thesong = [self.songs objectAtIndex:i];
CGRect frame;
frame.size.width=coverWidth; frame.size.height=coverHeight;
frame.origin.x=x; frame.origin.y=y;
LazyImageView* asyncImage = [[[LazyImageView alloc] initWithFrame:frame]autorelease];
NSURL* url = [NSURL URLWithString:thesong.cover];
[asyncImage loadImageFromURL:url];
UILabel *label = [[[UILabel alloc]initWithFrame:CGRectMake(x, y+artistLabelYPos, artistLabelWidth, artistLabelHeight)]autorelease];
label.text = [thesong.title stringByAppendingString:[@"\nby " stringByAppendingString:thesong.artist]];
[label setTextAlignment:UITextAlignmentLeft];
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:artistLabelFontSize];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkTextColor];
label.alpha = 0.75;
UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
playBtn.frame = CGRectMake(x+playBtnXPos, y+playBtnYPos, playBtnWidth, playBtnHeight);
[playBtn addTarget:self action:@selector(playBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
if(playingButton && streamer){
if(playingButtonTag == i && [streamer isPlaying]){
[playBtn setBackgroundImage:[UIImage imageNamed:pauseBtnimgName] forState:UIControlStateNormal];
playingButton = playBtn;
}else [playBtn setBackgroundImage:[UIImage imageNamed:playBtnimgName] forState:UIControlStateNormal];
}else [playBtn setBackgroundImage:[UIImage imageNamed:playBtnimgName] forState:UIControlStateNormal];
playBtn.tag = i;
[cell.contentView addSubview:asyncImage];
[cell.contentView addSubview:label];
[cell.contentView addSubview:playBtn];
label = nil;
asyncImage = nil;
if(curCellNo == 0){
y += estCoverHeight;
if(y>=(estCoverHeight*noOfRowsInCell)){
col++;
x += estCoverWidth;
if(col>=3)
y=0;
else
y=yForFirst;
}
}else{
col =3;
y += estCoverHeight;
if(y>=(estCoverHeight*noOfRowsInCell)){
x += estCoverWidth;
y=0;
}
}
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.contentView.transform = CGAffineTransformMakeRotation(M_PI/2);
return cell;
}
你可以发布你的`tableView:cellForRowAtIndexPath:`方法的实现吗? – filipe 2010-11-24 19:36:14