扩展的 UITableViewCell 无法正常工作?
Expanded UITableViewCell not working properly?
我正在处理 objective 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];
}
任何人都可以建议我...
我得到这样的输出..
这是我的输出图像
我想这样输出
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];
}
我正在处理 objective 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];
}
任何人都可以建议我... 我得到这样的输出..
这是我的输出图像
我想这样输出
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];
}