iOS:部分重叠UITableView记录

时间:2014-04-08 09:38:10

标签: ios uitableview

我试图在UITableView中显示三个部分(@"帐户和任务",@"生活事件",@"更多")和相关内容。我正在为每一行创建一个自定义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;
}

1 个答案:

答案 0 :(得分:1)

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

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