gpt4 book ai didi

iphone - 表格单元格的奇怪行为

转载 作者:行者123 更新时间:2023-11-28 20:20:38 24 4
gpt4 key购买 nike

我只是在我的应用程序中实现一个表格 View 。我已经这样做了很多次,但这次表格单元格显示重复数据,表格滚动的速度也不好。只是发布我的表格代码。任何有助于防止单元格数据重复和滚动速度的帮助 优化将不胜感激。

- (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;
}

Things updated in cell 是为了在单个单元格中添加 3 个按钮。没有别的。

最佳答案

建议

使用自定义单元格

修复:

创建和添加 subview 要包含在分配单元格的大括号内

    if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier];
//CREATING AND ADDING SUBVIEW SHOULD BE HERE
}
//Change in values is taken care here

原因

否则每次创建 subview 时,分配所有对象并在表格滚动时添加到上面

引用

Apple Docs

关于iphone - 表格单元格的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16185572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com