gpt4 book ai didi

ios - 第一个单元格变空,滚动后所有单元格文本消失

转载 作者:行者123 更新时间:2023-11-29 10:31:30 24 4
gpt4 key购买 nike

我正在使用 UITableView 并使用自定义单元格。我正在设置 UILabel 。我的 UITableView 的第一行是空的。UILabel 从第二行开始。向上滚动后 UILabel 消失。向下滚动 UILabel 出现。我们如何解决这个问题。在 View 上加载

    self.automaticallyAdjustsScrollViewInsets = NO;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"CustumCell";

CustumCell *cell = (CustumCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

}

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200 ,200 )];
label.text=@"hello";
[cell.contentView addSubview:label];

return cell;
}
- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 40;
}

最佳答案

尝试使用下面的代码

CustumCell *cell = (CustumCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
cell = [nib objectAtIndex:0];

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200 ,200 )];
label.tag=1;
[cell.contentView addSubview:label];

}

UILabel *label = (UILabel *)[cell.contentView viewWithTag:1];
label.text=@"hello";

无需每次都分配标签。你应该只在单元格为零时分配它。对于其他人尝试使用标签获取标签

关于ios - 第一个单元格变空,滚动后所有单元格文本消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367323/

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