[英]Issue on TableView Cell
Here i'm developing a expanded TableViewCell
,while TableView didSelectRowAtIndexPath
method i am using to cell expand, here i used two TableView Cell's 这里我正在开发一个扩展的
TableViewCell
,而我正在使用TableView didSelectRowAtIndexPath
方法进行单元扩展,这里我使用了两个TableView Cell
but i need to the button action SecondCell will be expanded.Can you please help how can i implement here is the below code, 但我需要将按钮动作SecondCell扩展。您能帮我如何在这里实现以下代码,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// disable touch on expanded cell
UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPath];
if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCellIdentifier"]) {
return;
}
// deselect row
[tableView deselectRowAtIndexPath:indexPath
animated:NO];
// get the actual index path
indexPath = [self actualIndexPathForTappedIndexPath:indexPath];
// save the expanded cell to delete it later
NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;
// same row tapped twice - get rid of the expanded cell
if ([indexPath isEqual:self.expandingIndexPath]) {
self.expandingIndexPath = nil;
self.expandedIndexPath = nil;
}
// add the expanded cell
else {
self.expandingIndexPath = indexPath;
self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
inSection:[indexPath section]];
}
[tableView beginUpdates];
if (theExpandedIndexPath) {
[_theTableView deleteRowsAtIndexPaths:@[theExpandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
if (self.expandedIndexPath) {
[_theTableView insertRowsAtIndexPaths:@[self.expandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
[tableView endUpdates];
// scroll to the expanded cell
[self.theTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
but i want implement while button taps secondCell will be Expanding. 但是我想在按钮点击secondCell时实现扩展。
- (IBAction)expand:(id)sender {
}
Can you please help me ,Thank you. 您能帮我吗,谢谢。
This is my solution which you can achieve the logic easily by creating a separate .xib file by disabling size classes. 这是我的解决方案,您可以通过禁用大小类来创建单独的.xib文件,从而轻松实现逻辑。 Use a boolean variable to check whether it is selected.
使用布尔变量检查是否已选择。 If so, then reload the specific cell and use a delegate method heightforrow and determine that boolean variable here.
如果是这样,则重新加载特定的单元格并使用委托方法heightforrow并在此处确定该布尔变量。 It is simple... Hope you understood.
很简单...希望您理解。 If not, please do let me know.
如果没有,请告诉我。 I'll share my code which I worked out.
我将分享我制定的代码。
Edited the post and below is my code :- 编辑帖子,下面是我的代码:-
@implementation ViewController
BOOL sel=NO;
NSMutableArray *a,*b;
NSIndexPath *path;
- (void)viewDidLoad {
[super viewDidLoad];
a=[[NSMutableArray alloc] initWithObjects:@"Apple",@"Mango",@"Orange",@"Pineapple",@"Cricket",@"Bike",@"Music", nil];
b=[[NSMutableArray alloc] initWithArray:a copyItems:YES];
[self.tview registerNib:[UINib nibWithNibName:@"cell1" bundle:nil] forCellReuseIdentifier:@"cell"];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return a.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView beginUpdates];
if([[a objectAtIndex:indexPath.row] isEqualToString:@"Selected"]){
sel=NO;
[a replaceObjectAtIndex:indexPath.row withObject:[b objectAtIndex:indexPath.row]];
}
else{
[a replaceObjectAtIndex:indexPath.row withObject:@"Selected"];
sel=YES;
}
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if([[a objectAtIndex:indexPath.row] isEqualToString:@"Selected"]){
return 100;
}
return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"cell";
cell1 *cell = (cell1 *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// If there is no cell to reuse, create a new one
/* if(cell == nil)
{
cell = [[listcustomcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}*/
if (!cell)
{
cell = [[cell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSLog(@"%f",[self tableView:self.tview heightForRowAtIndexPath:indexPath]);
cell.btn.frame=CGRectMake(cell.btn.frame.origin.x, [self tableView:self.tview heightForRowAtIndexPath:indexPath]*0.3, cell.btn.frame.size.width*1.3, cell.btn.frame.size.height);
UIButton *b=[[UIButton alloc] initWithFrame:cell.btn.frame];
[cell.btn removeFromSuperview];
[b setTitle:@"button" forState:UIControlStateNormal];
[cell.contentView addSubview:b];
[b addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void)checkButtonTapped:(id)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tview];
NSIndexPath *indexPath = [self.tview indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil)
{
//Just make sure that you've selected no selection option for the tableview.
[self tableView:self.tview didSelectRowAtIndexPath:indexPath];
NSLog(@"You've selected a row %ld",(long)indexPath.row);
}
}
@end
Create a separate .xib and place the button there. 创建一个单独的.xib并将按钮放在此处。 For resizing the button's frame, You need to remove it and add again while refreshing.
要调整按钮框架的大小,您需要将其删除并在刷新时再次添加。 Hope it helps :)
希望能帮助到你 :)
I go through you code , and i think you need only to get the cell. 我遍历了您的代码,我认为您只需要获取该单元格即可。 see the below code for getting it.
请参阅以下代码以获取它。
CGPoint touchPoint = [textField convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
Now do the same steps what you did in didSelectRowAtIndexPath
. 现在,执行与
didSelectRowAtIndexPath
相同的步骤。
If you want to expand/collapse your tableView cell then, I have made a demo that gives you the exact behaviour as you want. 如果您想扩展/折叠tableView单元格,那么我做了一个演示,可以为您提供所需的确切行为。 here is the link: https://github.com/rushisangani/TableViewCellExpand
这是链接: https : //github.com/rushisangani/TableViewCellExpand
Please set constraint of your expand/collapse views as described in demo. 请按照演示中的说明设置展开/折叠视图的约束。
Its based on the Content Hugging and Content Compression Resistance Priority . 基于内容拥抱和内容压缩抗性优先级的 。
- (IBAction)expand:(id)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.theTableView];
NSIndexPath *indexPathInner = [self.theTableView indexPathForRowAtPoint:buttonPosition];
// disable touch on expanded cell
UITableViewCell *cell = [self.theTableView cellForRowAtIndexPath:indexPathInner];
if ([[cell reuseIdentifier] isEqualToString:@"ExpandedCellIdentifier"]) {
return;
}
// deselect row
[tableView deselectRowAtIndexPath:indexPath
animated:NO];
// get the actual index path
indexPath = [self actualIndexPathForTappedIndexPath:indexPath];
// save the expanded cell to delete it later
NSIndexPath *theExpandedIndexPath = self.expandedIndexPath;
// same row tapped twice - get rid of the expanded cell
if ([indexPath isEqual:self.expandingIndexPath]) {
self.expandingIndexPath = nil;
self.expandedIndexPath = nil;
}
// add the expanded cell
else {
self.expandingIndexPath = indexPath;
self.expandedIndexPath = [NSIndexPath indexPathForRow:[indexPath row] + 1
inSection:[indexPath section]];
}
[tableView beginUpdates];
if (theExpandedIndexPath) {
[_theTableView deleteRowsAtIndexPaths:@[theExpandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
if (self.expandedIndexPath) {
[_theTableView insertRowsAtIndexPaths:@[self.expandedIndexPath]
withRowAnimation:UITableViewRowAnimationNone];
}
[tableView endUpdates];
// scroll to the expanded cell
[self.theTableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionMiddle
animated:YES];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.