在UITableview中加载数据问题
我使用UITableView
创建了一个应用程序。我解析来自URL的数据并在UITableView
中列出。在UITableview中加载数据问题
但是在UITableView
中,第一个数据只显示一次又一次。
我不知道为什么会出现这个问题。
我的代码如下:
-(UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
CGRect frame = CGRectMake(10.0f, 5.0, 300.0, 55);
valueField = [[UILabel alloc] initWithFrame:frame];
[valueField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
valueField.tag = 111;
valueField.font=[UIFont fontWithName:@"Helvetica" size:16.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode=UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
valueField.text=[title1 objectAtIndex:count1];
[cell.contentView addSubview:valueField];
UIImage *favOn = [UIImage imageNamed:@""];
UIImage *favOff = [UIImage imageNamed:@""];
titlebutton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, 15.0, 300.0, 55)];
[titlebutton setTag:indexPath.row];
//titlebutton.tag = 111;
[titlebutton setImage:favOff forState:UIControlStateNormal];
[titlebutton setImage:favOn forState:UIControlStateSelected];
if([self getFavState:@"111"])
{
[titlebutton setSelected:YES];
}
else
{
[titlebutton setSelected:NO];
}
[titlebutton addTarget:self action:@selector(favButtonSwitch:) forControlEvents:UIControlEventTouchUpInside];
[titlebutton setBackgroundColor:[UIColor clearColor]];
[titlebutton setTitle:@"" forState:UIControlStateNormal];
[cell.contentView addSubview:titlebutton];
CGSize labelSize = [valueField.text sizeWithFont:valueField.font constrainedToSize:valueField.frame.size lineBreakMode:UILineBreakModeWordWrap];
int labelHeight = labelSize.height;
NSLog(@"labelHeight = %d", labelHeight);
frame = CGRectMake(10.0f, 40.0, 300.0, 55);
pubField = [[UILabel alloc] initWithFrame:frame];
[pubField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
pubField.tag = 111;
pubField.font=[UIFont fontWithName:@"Helvetica" size:12.0];
pubField.textAlignment = UITextAlignmentLeft;
pubField.lineBreakMode=UILineBreakModeWordWrap;
pubField.numberOfLines = 0;
pubField.textColor = [UIColor whiteColor];
pubField.highlightedTextColor = [UIColor whiteColor];
pubField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
pubField.adjustsFontSizeToFitWidth = YES;
pubField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
pubField.text=[pubDate objectAtIndex:indexPath.row];
[cell.contentView addSubview:pubField];
frame = CGRectMake(5.0, 70.0, 300.0, 55);
desField = [[UILabel alloc] initWithFrame:frame];
[desField setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
desField.tag = 111;
desField.font=[UIFont fontWithName:@"Helvetica" size:13.0];
desField.textAlignment = UITextAlignmentLeft;
desField.lineBreakMode=UILineBreakModeWordWrap;
desField.numberOfLines = 0;
desField.textColor = [UIColor whiteColor];
desField.highlightedTextColor = [UIColor whiteColor];
desField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
desField.adjustsFontSizeToFitWidth = YES;
desField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
desField.text=[description objectAtIndex:indexPath.row];
[cell.contentView addSubview:desField];
}
UIImage *patternImage = [UIImage imageNamed:@"bg.jpg"];
UIView *backView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
backView.backgroundColor = [UIColor colorWithPatternImage: patternImage];
cell.backgroundView = backView;
NSString *image1 =[images objectAtIndex:indexPath.row];
NSLog(@"Images ..... = %@",image1);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:indexPath.row]]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
UIImageView *imageView = [ [ UIImageView alloc ] initWithImage: myimage ];
imageView.frame = CGRectMake(10.0f, 110.0f, 120.0f, 120.0f);
[cell.contentView addSubview:imageView];
return cell;
}
做所有的分配/ init和所有的东西,这将是相同的任意单元格中
if(cell==nil){
//<-- here
}
而设置的文本和由具体的数据块外改变任何事情
if(cell==nil){
}
//<-- here
即:
if(cell==nil){
//....
valueField.tag = 111;
valueField.font = [UIFont fontWithName:@ "Helvetica" size:16.0];
valueField.textAlignment = UITextAlignmentLeft;
valueField.lineBreakMode = UILineBreakModeWordWrap;
valueField.numberOfLines = 0;
valueField.textColor = [UIColor whiteColor];
valueField.highlightedTextColor = [UIColor whiteColor];
valueField.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
valueField.adjustsFontSizeToFitWidth = YES;
valueField.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
[cell.contentView addSubview : valueField];
//....
}
//....
valueField.text = [title1 objectAtIndex:count1];
//....
因为当你初始化并创建可重复使用的电池,你填写的数据有第一个单元格的数据是越来越显示在所有的细胞,你需要初始化一个可重复使用没有设置其所有值的单元格,然后为每个单元格设置此信息。也就是说,您需要为此代码的if(cell==nil)
部分以外的每个特定单元获取所需的所有信息,并依靠indexPath来了解您所在的单元,并根据此信息输入不同的数据。
当没有可重用的单元格可用时,您似乎正在构造单元格的子视图。问题是,当你的代码做出队一个可重用的单元时,你的代码是做什么的?看来你什么都不做。您应该移动任何特定于行的初始化(设置标签文本值,图像等)外部的检查出列单元格是否为nil
。通过这种方式,无论您是刚构建一个新的单元格还是先前创建的单元格,都会正确显示的数据与正在显示的行相关联。
其实你的代码这种方式很难解码,
但首先,你声明了很多标签,应该用来检索你声明的UIViews。 不幸的是这些都是一样的(标签),所以你可能永远不会得到你想要的。你应该在你的头文件中加入一些常量:
#define kPubLabel 100
然后使用它们。
然后你通常在声明你的单元格填充它们之后检索这些。
与此同时,通过此方法从url中检索图片是让您的应用程序变慢的最佳方式之一,因为每次滚动时,您都必须从其来源处重新获取图片从导致您的表视图“冻结”。
最后,根据你的代码,唯一可能改变的是你添加到你的框架的imageView。您通常拥有一个ImageView,您可以在其中放置可能已存储/缓存但很少更改单元格的contentView的图像。
ie
也许你应该在一个类中重写UITableViewCell的外部化单元构建代码。 将不同的标签放在您单元格的每个元素上,以便在您的方法中检索它们。
根据那里的标签检索你的元素在你的方法中。
填充并仅在您的方法中填充它们。
检索您的图像一劳永逸的表现改善。
我不确定这会有帮助,但以我的观点来看,在您的代码中实际上有很多工作要做。
感谢vikingosegundo。为了您的帮助,我解决了我的问题..谢谢 – Velmurugan 2010-11-17 08:44:05