我有一個UITableView
數據顯示,但當它滾動數據(UILabel
)要麼消失,要麼他們一遍又一遍地重複添加在彼此的頂部。每當我滾動每一次細胞交換數據。UITableView數據顯示多次滾動
這裏是我的cellForRowAtIndexPath:
代碼
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// configure the cell's background
UIImageView *gradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient"]];
[cell.contentView addSubview:gradient];
// configure the cell's label
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 300, 44)];
// grab a reference to the label's text from the tableData
nameLabel.text = [name objectAtIndex:indexPath.row];
nameLabel.textColor = [UIColor blackColor];
nameLabel.font = [UIFont fontWithName:@"DIN-Bold" size:12];
nameLabel.backgroundColor = [UIColor clearColor];
// set the autoReiszing mask -- this way if the label spills over the editing
// [icon?] then the text will trail off in ...
nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:nameLabel];
// configure the cell's label
UILabel *tableTextViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 220, 50)];
// grab a reference to the label's text from the tableData
tableTextViewLbl.text = [message objectAtIndex:indexPath.row];
tableTextViewLbl.textColor = [UIColor blackColor];
tableTextViewLbl.font = [UIFont fontWithName:@"DIN" size:10];
tableTextViewLbl.backgroundColor = [UIColor clearColor];
tableTextViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:tableTextViewLbl];
// configure the cell's label
UILabel *tableTimeStampViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 200, 50)];
// grab a reference to the label's text from the tableData
tableTimeStampViewLbl.text = [timeStamp objectAtIndex:indexPath.row];
tableTimeStampViewLbl.textColor = [UIColor lightGrayColor];
tableTimeStampViewLbl.font = [UIFont fontWithName:@"DIN" size:7];
tableTimeStampViewLbl.backgroundColor = [UIColor clearColor];
tableTimeStampViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:tableTimeStampViewLbl];
// UIImageView *image;
// UIImage *image1=[UIImage imageNamed:@"rest.png"];
// image=[[UIImageView alloc]initWithImage:image1];
// image.frame=CGRectMake(10,30,40,30);
//
// [cell.contentView addSubview:image];
//
return cell;
}
不,請不要接受這個答案。這可能是你在這種情況下可能做的最糟糕的事情。你幾乎可以保證有內存問題,表格會非常緩慢地工作。請投票回答。使用@Stavash答案,這是正確的,並將解決您的問題。 – Fogmeister