Is it possible to programmatically show the red delete button on a UITableViewCell?

后端 未结 4 1912
傲寒
傲寒 2020-12-29 21:41

There are lots of similar questions on here, but none that I think specifically ask this question, which is, is there any way in code to force the red delete button to appea

相关标签:
4条回答
  • 2020-12-29 22:08

    By calling

    [self.tableView setEditing:YES animated:YES];
    

    on a UITableViewController you can set the tableView to editing mode. you can actually add a gesture recognizer to the cell a cell view that makes that call using a delegate method.

    According to the Documentation of UITableViewCell you can actually set the same way an individual cell. Apart from that you will have to manage the standard gesture recognizer. I guess subclassing would be your ally on that situation.

    I hope this helps.

    0 讨论(0)
  • 2020-12-29 22:26
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *CellIdentifier = [NSString stringWithFormat:@"cell %d",indexPath.row];
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            if(TableMethod==1)
            {
                UIButton *delete_btn=[UIButton buttonWithType:UIButtonTypeCustom];
                [delete_btn setFrame:CGRectMake(10,15,25,25)];
                delete_btn.tag=indexPath.row;
                [delete_btn setImage:[UIImage imageNamed:@"remove_Icone.png"] forState:UIControlStateNormal];
                [delete_btn addTarget:self action:@selector(delete_btn:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:delete_btn];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    
        // Configure the cell.
        return cell;
    }
    
    
    - (IBAction)delete_btn:(id)sender
    {
        UIButton *buttontag = (UIButton *)sender;
        //NSLog(@"%d",buttontag.tag);
        Delete_row = buttontag.tag;
    
        UIAlertView *Alert = [[UIAlertView alloc]initWithTitle:@"Delete Reservation"   message:@"Are you sure to want Delete Reservation ??" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel",nil];
        Alert.tag=1;
        [Alert show];
        [Alert release];
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    {
        if(alertView.tag==1)
        {
            if(buttonIndex == 0)
            {
                // delete row
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 22:30

    In the doubleFingerSwipe gesture's selector method set one variable and assign as no of row swiped and reload the table . In the

    - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if(indexPath.row == yourVariable) return UITableViewCellEditingStyleDelete;
        else return UITableViewCellEditingStyleNone;
    }
    

    I think thats work for ur problem.

    0 讨论(0)
  • 2020-12-29 22:35

    This is not an answer to your question, but a suggestion about the functionality (I can't leave comments yet, but I would still like to offer my two cents).

    From a UI/UX perspective, overriding the default or "expected" functionality is not a great idea. Users have certain expectations about how various apps and features will work, and when they don't work in that manner, the users either get frustrated or confused. Hence the proliferation of the pull-to-refresh functionality in nearly every app that involves a table view. What would make more sense would be to have the two-finger swipe be the custom functionality. Not only would your app then conform to traditional UX guidelines (in fact, I think Apple might reject your app as it stands, and that is probably why you're having such a hard time finding an answer), but it would involve a lot less struggle/custom code.

    So sorry that wasn't a direct answer to your question, but I hope it helps.

    0 讨论(0)
提交回复
热议问题