gpt4 book ai didi

iOS:部分与 UITableView 记录重叠

转载 作者:行者123 更新时间:2023-11-29 03:04:33 25 4
gpt4 key购买 nike

我正在尝试在 UITableView 中显示三个部分(@"Accounts and Tasks"、@"Life Events"、@"More")和相关内容。我正在为每一行创建一个自定义 UILabel 并尝试显示文本。

下面是我的代码。我的问题是,第三部分没有显示第三部分内容“社交媒体”、@“关于 Hop、@”设置,而是显示一些第一部分内容并重叠文本。

有人可以指导我解决这个问题吗?

- (void)viewDidLoad
{
[super viewDidLoad];

// Do any additional setup after loading the view from its nib.
NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:@"My Accounts",@"Transfers/Deposit",@"Pay Bills", @"Investment Center",@"Claims Center",@"Locators",@"Loan Calculator", nil];
NSArray *arrTemp2 = [[NSArray alloc]initWithObjects:@"Auto Circle",@"Home Circle",nil];
NSArray *arrTemp3 = [[NSArray alloc]initWithObjects:@"Social Media",@"About Hop", @"Settings", nil];


self.keys = [NSArray arrayWithObjects:@"Accounts and Tasks", @"Life Events", @"More", nil];
NSArray *values = [NSArray arrayWithObjects:arrTemp1, arrTemp2, arrTemp3, nil];

self.tableContents = [NSDictionary dictionaryWithObjects:values forKeys:keys];

NSLog(@"table %@",self.tableContents);
NSLog(@"table with Keys %@",[self.tableContents allKeys]);

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.keys count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.keys objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:[self.keys objectAtIndex:section]];
return [listData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

NSArray *listData =[self.tableContents objectForKey:[self.keys objectAtIndex:[indexPath section]]];

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

if(cell == nil) {

NSLog(@"listData: %@", listData);

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(26, 4, 250, 30)];

label.text = [listData objectAtIndex:[indexPath row]];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];

}
[cell setUserInteractionEnabled:YES];

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"SettingsMore"];
[cell.contentView addSubview:imv];


return cell;
}

最佳答案

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
UILabel *label;
if(cell == nil) {
label = [[UILabel alloc] initWithFrame:CGRectMake(26, 4, 250, 30)];

label.tag = 8888;
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
}
label = (UILabel *)[cell.contentView viewWithTag:8888];
label.text = [listData objectAtIndex:[indexPath row]];
}

如果您在 if(cell == nil) 中设置了 label.text,文本将不会在重复使用该单元格时更新。

关于iOS:部分与 UITableView 记录重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22933142/

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