选择 UITableView 时使内容可见
Make Content visible when UITableView is selected
我正在使用此方法在选中时放大我的单元格的高度。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath isEqual:[tableView indexPathForSelectedRow]]) {
return 100.0;
}
return 44.0;
}
我想在单元格放大时才可见的区域显示一个UIImageView
,取消选择单元格时隐藏。
您的代码看起来可以正确放大单元格。
您也可以按照下面提到的步骤进行同样的操作 -
1) 添加一个 属性 以跟踪所选单元格
@property (nonatomic) int currentSelectedRow;
2) 用一个值初始化它
self.currentSelectedRow = -1;
3) Return 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath row] == self.currentSelectedRow) {
return 100.0;
}
return 44.0;
}
4) 单元格选择方式设置选中行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// code to do on cell selection
// set the row number of current selected cell
self.currentSelectedRow = indexPath.row;
// animate the selected row
[tableView beginUpdates];
[tableView endUpdates];
}
5) 在单元格取消选择方法中将选中的行设置为无效行号(-1)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
// invalid row number
self.currentSelectedRow = -1;
// animate the selected row
[tableView beginUpdates];
[tableView endUpdates];
}
注意:这只是一个示例代码,您可以根据自己的需要进行定制。
这是一个简单的例子
NSInteger selectedIndex;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITableView *tbl=[[UITableView alloc]initWithFrame:CGRectMake(0, 75, 320, 300)];
[tbl setDelegate:self];
[tbl setDataSource:self];
[self.view addSubview:tbl];
selectedIndex=-1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 8;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[[UITableViewCell alloc]init];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 70, 40)];
[lbl setText:@"cell"];
[cell addSubview:lbl ];
UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(100, 6, 50, 30)];
[imgVw setBackgroundColor:[UIColor clearColor]];
[imgVw setTag:400+indexPath.row];
[imgVw setAlpha:0.0];
[cell addSubview:imgVw];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
UIImageView *imgVwCellBg=(UIImageView*)[cell viewWithTag:400+indexPath.row];
if (selectedIndex==indexPath.row)
{
selectedIndex=-1;
[imgVwCellBg setAlpha:0.0];
}
else
{
selectedIndex=indexPath.row;
[imgVwCellBg setBackgroundColor:[UIColor brownColor]];
[imgVwCellBg setAlpha:1.0];
}
[tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
UIImageView *imgVwCellBg=(UIImageView*)[cell viewWithTag:400+indexPath.row];
selectedIndex=-1;
[imgVwCellBg setAlpha:0.0];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndex==indexPath.row)
{
return 100;
}
else
{
return 44;
}
}
试一次
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (cell == nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 200, 0)];
[imageView setTag:1000];
[imageView setImage:[UIImage imageNamed:@"Image_2"]];
[cell addSubview:imageView];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1000];
if (selectedIndex==indexPath.row)
{
selectedIndex=-1;
[UIView animateWithDuration:0.4 animations:^{
[imageView setFrame:CGRectMake(20, 5, 200, 0)];
}];
}
else
{
selectedIndex = indexPath.row;
[UIView animateWithDuration:0.4 animations:^{
[imageView setFrame:CGRectMake(20, 5, 200, 170)];
}];
}
[tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
[UIView animateWithDuration:0.4 animations:^{
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1000];
[imageView setFrame:CGRectMake(20, 5, 200, 0)];
}];
[tableView endUpdates];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndex == indexPath.row)
{
return 200;
}
else
{
return 75;
}
return 0;
}
我认为问题可能是 NSIndexPath 的 isEuqal:
方法,不像 NSString,你不能只用用户 isEuqal:
来判断 NSIndexPath 是否相等。
if([indexPath isEqual:[tableView indexPathForSelectedRow]]) {
替换为
NSIndexPath *selectedPath = [tableView indexPathForSelectedRow];
if(selectedPath.row == indexPath.row && selectedPath.section == indexPath.section){
好的,这就是我所做的,使用你给我的一切并做到这一点
@property (nonatomic) int currentSelectedRow;
- (void)viewDidLoad {
[super viewDidLoad];
self.currentSelectedRow = -1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellId = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304];
contentView.hidden = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
self.currentSelectedRow = indexPath.row;
[tableView beginUpdates];
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304];
contentView.hidden = NO;
if (_currentSelectedRow == indexPath.row)
{
[UIView animateWithDuration:0.4 animations:^{
[contentView setFrame:CGRectMake(20, 5, 200, 0)];
}];
}
else
{
_currentSelectedRow = indexPath.row;
[UIView animateWithDuration:0.4 animations:^{
contentView.hidden = NO;
[contentView setFrame:CGRectMake(20, 5, 200, 170)];
}];
}
[tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304];
contentView.hidden = YES;
// invalid row number
self.currentSelectedRow = -1;
// animate the selected row
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == self.currentSelectedRow) {
return 100.0;
}
return 44.0;
}
感谢您的参与
一定要给这个问题投票,它会对很多人有帮助
我正在使用此方法在选中时放大我的单元格的高度。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath isEqual:[tableView indexPathForSelectedRow]]) {
return 100.0;
}
return 44.0;
}
我想在单元格放大时才可见的区域显示一个UIImageView
,取消选择单元格时隐藏。
您的代码看起来可以正确放大单元格。
您也可以按照下面提到的步骤进行同样的操作 -
1) 添加一个 属性 以跟踪所选单元格
@property (nonatomic) int currentSelectedRow;
2) 用一个值初始化它
self.currentSelectedRow = -1;
3) Return 单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([indexPath row] == self.currentSelectedRow) {
return 100.0;
}
return 44.0;
}
4) 单元格选择方式设置选中行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// code to do on cell selection
// set the row number of current selected cell
self.currentSelectedRow = indexPath.row;
// animate the selected row
[tableView beginUpdates];
[tableView endUpdates];
}
5) 在单元格取消选择方法中将选中的行设置为无效行号(-1)
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
// invalid row number
self.currentSelectedRow = -1;
// animate the selected row
[tableView beginUpdates];
[tableView endUpdates];
}
注意:这只是一个示例代码,您可以根据自己的需要进行定制。
这是一个简单的例子
NSInteger selectedIndex;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITableView *tbl=[[UITableView alloc]initWithFrame:CGRectMake(0, 75, 320, 300)];
[tbl setDelegate:self];
[tbl setDataSource:self];
[self.view addSubview:tbl];
selectedIndex=-1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 8;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[[UITableViewCell alloc]init];
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, 70, 40)];
[lbl setText:@"cell"];
[cell addSubview:lbl ];
UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(100, 6, 50, 30)];
[imgVw setBackgroundColor:[UIColor clearColor]];
[imgVw setTag:400+indexPath.row];
[imgVw setAlpha:0.0];
[cell addSubview:imgVw];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
UIImageView *imgVwCellBg=(UIImageView*)[cell viewWithTag:400+indexPath.row];
if (selectedIndex==indexPath.row)
{
selectedIndex=-1;
[imgVwCellBg setAlpha:0.0];
}
else
{
selectedIndex=indexPath.row;
[imgVwCellBg setBackgroundColor:[UIColor brownColor]];
[imgVwCellBg setAlpha:1.0];
}
[tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
UIImageView *imgVwCellBg=(UIImageView*)[cell viewWithTag:400+indexPath.row];
selectedIndex=-1;
[imgVwCellBg setAlpha:0.0];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndex==indexPath.row)
{
return 100;
}
else
{
return 44;
}
}
试一次
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (cell == nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 200, 0)];
[imageView setTag:1000];
[imageView setImage:[UIImage imageNamed:@"Image_2"]];
[cell addSubview:imageView];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1000];
if (selectedIndex==indexPath.row)
{
selectedIndex=-1;
[UIView animateWithDuration:0.4 animations:^{
[imageView setFrame:CGRectMake(20, 5, 200, 0)];
}];
}
else
{
selectedIndex = indexPath.row;
[UIView animateWithDuration:0.4 animations:^{
[imageView setFrame:CGRectMake(20, 5, 200, 170)];
}];
}
[tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
[tableView beginUpdates];
[UIView animateWithDuration:0.4 animations:^{
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1000];
[imageView setFrame:CGRectMake(20, 5, 200, 0)];
}];
[tableView endUpdates];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndex == indexPath.row)
{
return 200;
}
else
{
return 75;
}
return 0;
}
我认为问题可能是 NSIndexPath 的 isEuqal:
方法,不像 NSString,你不能只用用户 isEuqal:
来判断 NSIndexPath 是否相等。
if([indexPath isEqual:[tableView indexPathForSelectedRow]]) {
替换为
NSIndexPath *selectedPath = [tableView indexPathForSelectedRow];
if(selectedPath.row == indexPath.row && selectedPath.section == indexPath.section){
好的,这就是我所做的,使用你给我的一切并做到这一点
@property (nonatomic) int currentSelectedRow;
- (void)viewDidLoad {
[super viewDidLoad];
self.currentSelectedRow = -1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellId = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304];
contentView.hidden = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
self.currentSelectedRow = indexPath.row;
[tableView beginUpdates];
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304];
contentView.hidden = NO;
if (_currentSelectedRow == indexPath.row)
{
[UIView animateWithDuration:0.4 animations:^{
[contentView setFrame:CGRectMake(20, 5, 200, 0)];
}];
}
else
{
_currentSelectedRow = indexPath.row;
[UIView animateWithDuration:0.4 animations:^{
contentView.hidden = NO;
[contentView setFrame:CGRectMake(20, 5, 200, 170)];
}];
}
[tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
UIImageView *contentView = (UIImageView *)[cell viewWithTag:304];
contentView.hidden = YES;
// invalid row number
self.currentSelectedRow = -1;
// animate the selected row
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == self.currentSelectedRow) {
return 100.0;
}
return 44.0;
}
感谢您的参与 一定要给这个问题投票,它会对很多人有帮助