2017-06-03 96 views
0

我正在处理目标c扩展UITableViewCell,它无法正常工作。我的代码是...扩展的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]; 
} 

任何一个可以建议我... 我收到输出这样的..

这是我的输出图像

我想像这样输出

+0

我想输出是正确的。根据你的问题是什么? –

+0

请看第二张图片.. – iOS

+0

在Name1我想要1,EmailID1,Name2:2,EmailID2 ...像这样... – iOS

回答

1

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

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]; 
} 
+0

是的,它工作正常。谢谢你.... – iOS

+0

不客气,;) –