Madan Mohan
Madan Mohan

Reputation: 8810

How to set editing is yes for a particular section in iPhone

I have declared UITableView in .h file. I have 5 section in the table view, now I need to set the tableview.editing= yes only for section 4.

How can I display the + button beside section 4 and have all other sections in editing = NO?

myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,320,415) style:UITableViewStyleGrouped];
        myTableView.delegate = self;
        myTableView.dataSource=self;
        [myTableView setSectionFooterHeight:0];
        [myTableView setSectionHeaderHeight:15];
        [myTableView setSeparatorColor:[UIColor lightGrayColor]];
        myTableView.backgroundColor = [UIColor colorWithRed:0.85546875 green:0.8828125 blue:0.92578125 alpha:1];
        [myView addSubview: myTableView];


I tried like this it is not working.
- (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section
{
    if(section==0)
    {
        myTableView.editing = NO;
        myTableView.allowsSelectionDuringEditing = NO;
        return 3;
    }
    else if(section ==1)
    {
        myTableView.editing = NO;
        myTableView.allowsSelectionDuringEditing = NO;
        return 2;
    }
    else if(section==4)
    {
        myTableView.editing = YES;
        myTableView.allowsSelectionDuringEditing = YES;
        if(isEdit == YES)
        {
            if([editObject.contactList count]>0)
            {
                return [editObject.contactList count]+1;
            }
            else
            {
                return 1;
            }
        }   
        else if(isEdit == NO)
        {
            if([addContactList count]>0)
            {
                return [addContactList count]+1;
            }
            else
            {
                return 1;
            }
        }

    }
    else if(section==2)
    {
        myTableView.editing = NO;
        myTableView.allowsSelectionDuringEditing = NO;
        return 1;
    }
    else if(section==3)
    {
        myTableView.editing = NO;
        myTableView.allowsSelectionDuringEditing = NO;
        return 1;
    }
    return 0;
}

I am getting the + button in the 4th section, but all other sections are moved to the right side.

Upvotes: 2

Views: 2168

Answers (2)

jrtc27
jrtc27

Reputation: 8526

Implement

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}

Upvotes: 2

nevan king
nevan king

Reputation: 113747

Implement - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath and return UITableViewCellEditingStyleNone for the rows in the sections you don't want to be editable.

Upvotes: 0

Related Questions