扩展的UITableViewCell无法正常工作?

时间:2017-06-03 05:43:27

标签: ios objective-c uitableview uitableviewsectionheader

我正在开展目标c展开UITableViewCell,但它无法正常工作。我的代码是......

self.nameArr = [[NSMutableArray alloc]initWithObjects: @"Name1", @"Name2", @"Name3", @"Name4", @"Name5", @"Name6", @"Name7", @"Name8", @"Name9", @"Name10", nil];
self.phoneNoArray = [[NSMutableArray alloc]initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9",  nil];
self.emailDArray = [[NSMutableArray alloc]initWithObjects:@"EMail ID1", @"EMail ID2", @"EMail ID3", @"EMail ID4", @"EMail ID5", @"EMail ID6", @"EMail ID7", @"EMail ID8", @"EMail ID9", nil];

//#pragma mark - TableView methods

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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ([arrSelectedSectionIndex containsObject:[NSNumber numberWithInteger:section]])
{
    return 2;
} else {
    return 0;
}
}

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

ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ViewControllerCell"];

if (cell ==nil)
{
    [tblView registerClass:[ViewControllerCell class] forCellReuseIdentifier:@"ViewControllerCell"];

    cell = [tblView dequeueReusableCellWithIdentifier:@"ViewControllerCell"];
}

for (int j=0; j<=self.i; j++) {

    if (indexPath.row == 0) {
        NSLog(@"Index path0 :%lu", indexPath.row);
        cell.lblName.text = [self.phoneNoArray objectAtIndex:self.i];

    } else {
        NSLog(@"Index path1 :%lu", indexPath.row);
        cell.lblName2.text = [self.emailDArray objectAtIndex:self.i];

    }
}

cell.backgroundColor = [UIColor blueColor];
return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44.0f;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
ViewControllerCellHeader *headerView = [tableView dequeueReusableCellWithIdentifier:@"ViewControllerCellHeader"];

if (headerView ==nil)
{
    [tblView registerClass:[ViewControllerCellHeader class] forCellReuseIdentifier:@"ViewControllerCellHeader"];

    headerView = [tableView dequeueReusableCellWithIdentifier:@"ViewControllerCellHeader"];
}

headerView.lbTitle.text = [self.nameArr objectAtIndex:section];

if ([arrSelectedSectionIndex containsObject:[NSNumber numberWithInteger:section]])
{
    headerView.btnShowHide.selected = YES;
}

//Assigning tags for buttons on tableView cells ...
headerView.btnShowHide.tag = section;
[[headerView btnShowHide] setTag:section];

[[headerView btnShowHide] addTarget:self action:@selector(btnTapShowHideSection:) forControlEvents:UIControlEventTouchUpInside];

//random color
[headerView.contentView setBackgroundColor:[UIColor clearColor]];

return headerView.contentView;
}

-(IBAction)btnTapShowHideSection:(UIButton*)sender
{
self.i = sender.tag;
if (!sender.selected)
{
    [arrSelectedSectionIndex addObject:[NSNumber numberWithInteger:sender.tag]];

    sender.selected = YES;
}else{
    sender.selected = NO;

    if ([arrSelectedSectionIndex containsObject:[NSNumber numberWithInteger:sender.tag]])
    {
        [arrSelectedSectionIndex removeObject:[NSNumber numberWithInteger:sender.tag]];
    }
}

[tblView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}

任何人都可以建议我...... 我得到这样的输出..

这是我的输出图像

我想要像这样放

1 个答案:

答案 0 :(得分:1)

image1中的问题看起来像是一个单元重用问题。尝试将 for loop 中的代码更改为:

if (indexPath.row == 0) {
    NSLog(@"Index path0 :%lu", indexPath.row);
    cell.lblName.text = [self.phoneNoArray objectAtIndex:self.i];
    cell.lblName2.text = @"";

} else {
    NSLog(@"Index path1 :%lu", indexPath.row);
    cell.lblName.text = @"";
    cell.lblName2.text = [self.emailDArray objectAtIndex:self.i];
}