我只是在我的應用程序中實現表格視圖。我已經多次這樣做了,但這次表格單元格顯示重複的數據和表格滾動的速度也不好。只需發佈我的代碼表。將意識到任何有助於防止細胞數據重複和滾動速度優化的優化。表格單元格的奇怪行爲
- (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個按鍵。沒有其他的。
您應該使用自定單元,而不是刪除所有子視圖。 – Bhavin 2013-04-24 07:15:25
@Vin直到現在我還沒有接近自定義單元格。但是如果問題不解決,我也會這樣處理。 – 2013-04-24 07:54:34