Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 关于TableView单元的问题_Ios_Objective C_Iphone_Xcode_Tableviewcell - Fatal编程技术网

Ios 关于TableView单元的问题

Ios 关于TableView单元的问题,ios,objective-c,iphone,xcode,tableviewcell,Ios,Objective C,Iphone,Xcode,Tableviewcell,这里我正在开发一个扩展的TableView单元格,而TableViewdidselectrowatinexpath方法我用来扩展单元格,这里我使用了两个TableView单元格 但是我需要说明的是,按钮操作SecondCell将被扩展。您能帮助我如何实现下面的代码吗 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // disable t

这里我正在开发一个扩展的
TableView单元格
,而TableView
didselectrowatinexpath
方法我用来扩展单元格,这里我使用了两个TableView单元格

但是我需要说明的是,按钮操作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];
}
但我想在电池扩张时实现

- (IBAction)expand:(id)sender {

}

你能帮我一下吗,谢谢。

我看了你的密码,我想你只需要拿到手机就行了。请参阅下面的代码以获取它

- (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];

}
CGPoint touchPoint = [textField convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

现在,按照您在
didSelectRowAtIndexPath

中所做的步骤进行操作。我仔细检查了您的代码,我认为您只需要获取单元格。请参阅下面的代码以获取它

CGPoint touchPoint = [textField convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

现在,按照您在
didselectrowatinexpath

中所做的步骤执行。这是我的解决方案,您可以通过禁用大小类创建单独的.xib文件来轻松实现逻辑。使用布尔变量检查是否选中它。如果是这样,则重新加载特定单元格并使用委托方法heightforrow,并在此处确定该布尔变量。这很简单。。。希望你能理解。如果没有,请告诉我。我会和大家分享我的代码

编辑了这篇文章,下面是我的代码:-

    @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

创建一个单独的.xib并将按钮放在那里。要调整按钮框架的大小,需要在刷新时将其删除并再次添加。希望有帮助:)

这是我的解决方案,您可以通过禁用大小类创建单独的.xib文件来轻松实现逻辑。使用布尔变量检查是否选中它。如果是这样,则重新加载特定单元格并使用委托方法heightforrow,并在此处确定该布尔变量。这很简单。。。希望你能理解。如果没有,请告诉我。我会和大家分享我的代码

编辑了这篇文章,下面是我的代码:-

    @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

创建一个单独的.xib并将按钮放在那里。要调整按钮框架的大小,需要在刷新时将其删除并再次添加。希望有帮助:)

如果您想展开/折叠tableView单元格, 我已经做了一个演示,让你的确切行为,因为你想要的。以下是链接:

请按照演示中的说明设置展开/折叠视图的约束


它基于内容拥抱和内容压缩阻力优先级

如果要展开/折叠tableView单元格, 我已经做了一个演示,让你的确切行为,因为你想要的。以下是链接:

请按照演示中的说明设置展开/折叠视图的约束


它基于内容拥抱和内容压缩抵抗优先级

Hi-Pradheep,谢谢你能分享你的代码这会很有帮助谢谢@Pradheep,我用这种方法解决了问题,但我只需要在按钮点击时将单元格展开,而不是didselectrowHi-Pradheep,谢谢,你能分享你的代码吗?这会很有帮助的谢谢@pradheep,我用这种方法解决了问题,但我只需要在按下按钮时单元格将被展开,而不是didselectrow