gpt4 book ai didi

ios - 当我在表格 View 中单击 2 个单元格时,自动折叠 1 个单元格

转载 作者:行者123 更新时间:2023-12-01 18:43:03 25 4
gpt4 key购买 nike

我已经为展开和折叠编写了代码,这里我的问题是,在表格 View 中,当我单击第一个单元格时,它正在正确展开,但是当我单击第二个单元格时,第一个单元格自动折叠。
我的代码是

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFilter)
{
return [searchArray count];
}
else
return [responceArray count];
}




-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Content1"];
if(!cell)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Content1"];

}

UILabel *namelab = (UILabel *)[cell viewWithTag:1];
UILabel *state = (UILabel *)[cell viewWithTag:2];
UILabel *status = (UILabel *)[cell viewWithTag:4];
UILabel *serialNo=(UILabel *)[cell viewWithTag:8];
UILabel *company = (UILabel *)[cell viewWithTag:5];
UIView *aview=(UIView *)[cell.contentView viewWithTag:10];
if(isFilter)
{

serialNo.text=[NSString stringWithFormat:@"%@",[[searchArray objectAtIndex:indexPath.row] valueForKey:@"job_id"]];
namelab.text=[[searchArray objectAtIndex:indexPath.row]valueForKey:@"maintenance_friendly_name"];
state.text=[[searchArray objectAtIndex:indexPath.row]valueForKey:@"site_id"];
status.text=[[searchArray objectAtIndex:indexPath.row]valueForKey:@"maintenance_status"];
company.text=[[searchArray objectAtIndex:indexPath.row]valueForKey:@"orgname"];
}
else
{
serialNo.text=[NSString stringWithFormat:@"%@",[[responceArray objectAtIndex:indexPath.row] valueForKey:@"job_id"]];
namelab.text=[[responceArray objectAtIndex:indexPath.row]valueForKey:@"maintenance_friendly_name"];
state.text=[[responceArray objectAtIndex:indexPath.row]valueForKey:@"site_id"];
status.text=[[responceArray objectAtIndex:indexPath.row]valueForKey:@"maintenance_status"];
company.text=[[responceArray objectAtIndex:indexPath.row]valueForKey:@"orgname"];

}
//alternative background colors for better division ;)

if (indexPath.row %2 ==1)
cell.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:1];
else
cell.backgroundColor = [UIColor colorWithRed:.8 green:.8 blue:.8 alpha:1];

// name.text = [cellTitles objectAtIndex:indexPath.row];

if (isTagsExapanded == YES && isSelectedRowIndex == indexPath.row) {


cell.contentView.frame = CGRectMake(0, 0,self.view.frame.size.width,160);

aview.backgroundColor=[UIColor lightGrayColor];

[cell.contentView viewWithTag:3].transform = CGAffineTransformMakeRotation(0);

}else{
cell.contentView.frame = CGRectMake(0, 0,self.view.frame.size.width,160);
aview.backgroundColor=[UIColor colorWithRed:1.00 green:1.00 blue:1.00 alpha:1.0];

}

return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

if (isTagsExapanded && isSelectedRowIndex == indexPath.row) {

return 160;

}else{

return 75;

}
return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


if (!isTagsExapanded ) {

isTagsExapanded = YES;
isSelectedRowIndex = indexPath.row;

}else{

isTagsExapanded = NO;

}
[_collapeExpandUITBV reloadData];

}

最佳答案

由于 isSelectedRowIndex,您的第一个单元格被禁用属性(property)。一次这将只保存一个索引,因此只有一个单元格将显示为展开状态,其余的单元格将根据您的代码折叠。

根据您的要求,它将需要维护一组选定的单元格索引。这是可能对您有所帮助的代码。

在 ViewController.h 文件中(或在类别定义中的 ViewController.m 文件中)

NSMutableArray *selectedCellIndexes;

在 ViewController.m 文件中:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if (selectedCellIndexes == nil) {
selectedCellIndexes = [[NSMutableArray alloc] init];
}

if(![selectedCellIndexes containsObject:[NSNumber numberWithInteger:indexPath.row]])
{
[selectedCellIndexes addObject:[NSNumber numberWithInteger:indexPath.row]]; // add if it does not exist
}
else
{
[selectedCellIndexes removeObject:[NSNumber numberWithInteger:indexPath.row]]; // remove if it exists
}
[tableView reloadData]; // redraw all cell again
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Cell alloc - init data here
...
...

if(selectedCellIndexes && [selectedCellIndexes containsObject:[NSNumber numberWithInteger:indexPath.row]])
{
// expanded cell code here
}
else {
// collapsed cell code here
}
return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int rowHeight = 0;
if(selectedCellIndexes && [selectedCellIndexes containsObject:[NSNumber numberWithInteger:indexPath.row]])
rowHeight = EXPANDED_CELL_HEIGHT; // define this value as constants
}
else
{
rowHeight = COLLAPSED_CELL_HEIGHT; // define this value as constants
}
return rowHeight;
}

希望这可以帮助。

关于ios - 当我在表格 View 中单击 2 个单元格时,自动折叠 1 个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40147060/

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