我有单独的定制UITableViewCell
用于显示数据(这些数据来自服务器 JSON 响应)。在每个UITableViewCell
我有“阅读更多”按钮。如果用户单击“阅读更多”按钮,我想以编程方式添加UILabel
用于显示来自服务器的附加信息。但最初我设置UITableViewCell
高度,因此单击“阅读更多”按钮后我无法看到附加信息UILabel
..
这是屏幕截图:
这是我需要的屏幕:
这是我使用的以下编码:
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
int height1;
if(readMore){
height1=200;
NSLog(@"Clicked");
}
else{
height1=100;
NSLog(@"Not clicked");
}
return height1; // Normal height
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [TitleArr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
static NSString *simpleTableIdentifier = @"SimpleTableCell_iPad";
cell = (TableCell_Leads *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
else{
static NSString *simpleTableIdentifier = @"TableCell_Leads";
cell = (TableCell_Leads *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
}
if (cell == nil)
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell_iPad" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
else{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableCell_Leads" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
}
cell.labTitle.text = [TitleArr objectAtIndex:indexPath.row];
cell.labCategory.text=[CategoryArr objectAtIndex:indexPath.row];
[cell.btnReadmore addTarget:self
action:@selector(funReadmore:)
forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (IBAction)funReadmore:(id)sender
{
[self.tableView beginUpdates];
readMore=TRUE;
NSLog(@"READ MORE");
[self.tableView endUpdates];
}
首先拿一个bool
& int
多变的。
BOOL isReadMoreButtonTouched = NO;
int indexOfReadMoreButton = -1;
然后用你的代码实现下面
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
[[cell btnReadmore] setTag:[indexPath row]];
if(isReadMoreButtonTouched && [indexPath row]== indexOfReadMoreButton)
{
// design your read more label here
}
}
现在实施IBAction
-(IBAction) funReadmore:(id)sender
{
UIButton *readMoreButton = (UIButton *)sender;
indexOfReadMoreButton=[readMoreButton tag];
isReadMoreButtonTouched=YES;
[[self tableView] beginUpdates];
[[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: indexOfReadMoreButton inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
[[self tableView] endUpdates];
}
现在来heightForRowAtIndexPath
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isReadMoreButtonTouched && [indexPath row]== indexOfReadMoreButton) return 200.0f;
else return 100.0f;
}
希望它对你有用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)