我需要在表格视图的一部分中单击按钮来展开框架和标签。同样,当再次单击按钮时,框架和标签应恢复到其原始(压缩)状态。这必须对表格视图的所有部分进行。如果我单击一个部分的按钮,它会扩展所有部分的框架。但是该标签已针对该单击部分上的所有内容展开。下面是代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger section = [indexPath section];
if(section==0 || section==2)
{
static NSString *myIdentifier = @"tableCell";
DUGSProjectTableCell *cell = (DUGSProjectTableCell*)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
cell.readMoreButton.tag=[indexPath section];
[cell.readMoreButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
if (section==0) {
cell.myLabel.text=@"On this festive season one of the leading real estate developers in NCR Delhi, is launching a new residential project ";
}
else if (section==2){
cell.myLabel.text=@"Situated in hot & fast upcoming neighbourhood,Spread in 30 acres,Located in the heart of the city";
}
if(self.isHeightChanged)
{
if ([cell.readMoreButton.titleLabel.text isEqualToString:@"Read More"]) {
[cell.readMoreButton setTitle:@"Read Less" forState:UIControlStateNormal];
cell.readMoreButton.frame=CGRectMake(223, 90 , 66, 25) ;
CGRect frame = cell.myLabel.frame;
cell.myLabel.numberOfLines=0;
cell.myLabel.lineBreakMode=NSLineBreakByWordWrapping;
frame.size.height=100;
cell.myLabel.frame=frame;
}
else if ([cell.readMoreButton.titleLabel.text isEqualToString:@"Read Less"])
{
[cell.readMoreButton setTitle:@"Read More" forState:UIControlStateNormal];
cell.readMoreButton.frame=CGRectMake(223, 70 , 66, 25) ;
CGRect frame = cell.myLabel.frame;
cell.myLabel.numberOfLines=0;
cell.myLabel.lineBreakMode=NSLineBreakByWordWrapping;
frame.size.height=30;
cell.myLabel.frame=frame;
}
}
return cell;
}
else if (section==1){
static NSString *myIdentifier = @"unitCell";
DUGSUnitPlanCell *cell = (DUGSUnitPlanCell*)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
return cell;
}
return NULL;
}
下面是按钮单击的代码:
(void)buttonPressed:(UIButton *)button
{
DUGSProjectTableCell *selectedCell = (DUGSProjectTableCell*)[[button superview] superview];
if (selectedCell) {
if ([button.titleLabel.text isEqualToString:@"Read More"] ) {
whichButton=@"M";
NSLog(@"%@",whichButton);
}
else if ([button.titleLabel.text isEqualToString:@"Read Less"]){
whichButton=@"L";
NSLog(@"%@",whichButton);
}
NSLog(@"%d",selectedCell.readMoreButton.tag);
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:selectedCell.readMoreButton.tag];
selectedIndexPath=indexPath;
NSLog(@"%@",selectedIndexPath);
self.isHeightChanged=YES;
[self.tableViewForPropertyOverview reloadRowsAtIndexPaths:@[selectedIndexPath ] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
下面是扩展框架的代码:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath section]==1){
return 60;
}
if (([indexPath section]==0)||([indexPath section]==2)) {
if (selectedIndexPath!=NULL){
return 120.0f;
}
else{
return 60.0f;
}}
}
所有部分的帧大小都增加到 120。相反,当单击其中的按钮时,它应该对第 0 和 2 部分执行此操作。请指教。