我只是在我的应用程序中实现一个表格视图。我已经多次这样做了,但这次表格单元格显示重复数据并且表格滚动的速度也不好。只是发布我的表格代码。任何有助于防止单元格数据重复和滚动速度优化的帮助将不胜感激。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = [array count];
if (count%3 == 0)
{
return count;
}
else
{
count = count/3+1;
}
NSLog(@"Row count is%i",count);
return count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
}
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 490, 221)];
int x = 15;
int y = 15;
int p = 10;
int q = 10;
for (int i = 0; i < 3; i++)
{
if ( indexPath.row*3+i < [array count] )
{
UIImageView *img = [[UIImageView alloc] init];
UIButton *btn = [[UIButton alloc] init];
[img setImageWithURL:[NSURL URLWithString:[array objectAtIndex:indexPath.row*3+i]]
placeholderImage:[UIImage imageNamed:@"India.png"]
success:^(UIImage *image) {}failure:^(NSError *error){}];
img.frame = CGRectMake(x, y, 140, 201);
btn.frame = CGRectMake(p, q, 150, 211);
btn.tag = indexPath.row*3+i;
[btn setBackgroundImage:[UIImage imageNamed:@"Picframe_N.png"] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"Picframe_S.png"] forState:UIControlStateHighlighted];
[btn addTarget:self action:@selector(Movie_detail:) forControlEvents:UIControlEventTouchUpInside];
[testView addSubview:img];
[testView addSubview:btn];
x = x+160;
p = p+160;
}
else
{
NSLog(@"No data");
break;
}
}
[cell addSubview:testView];
return cell;
}
单元格中更新的内容是为了在单个单元格中添加 3 个按钮。没有其他的。