我选择使用此方法扩大单元格的高度。
- (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
,只有在单元格被放大时才可见,我希望在取消选择单元格时隐藏它。
答案 0 :(得分:1)
您的代码看起来正确放大单元格。
您也可以按照以下步骤进行相同操作 -
1)添加属性以跟踪所选单元格
@property (nonatomic) int currentSelectedRow;
2)用值
初始化它self.currentSelectedRow = -1;
3)返回细胞高度
- (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];
}
注意:这只是一个示例代码,您可以根据自己的要求进行自定义。
答案 1 :(得分:1)
这是一个简单的例子
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;
}
}
答案 2 :(得分:1)
尝试一次
- (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;
}
答案 3 :(得分:1)
我认为问题可能是NSIndexPath的isEuqal:
方法,而不像NSString,你不能只是用户isEuqal:
来判断NSIndexPath是否相等。
if([indexPath isEqual:[tableView indexPathForSelectedRow]]) {
替换为
NSIndexPath *selectedPath = [tableView indexPathForSelectedRow];
if(selectedPath.row == indexPath.row && selectedPath.section == indexPath.section){
答案 4 :(得分:0)
好的所以这就是我所做的,用你给我的所有东西
@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;
}
感谢您的所有投入 一定要投票支持它会帮助很多人的问题