Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • selecting multiple rows in a UITableView

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 223
    Comment on it

    The UITableView is the most used tool by the iPhone developers to display various types of data in a neat, clean and easy to manage format. The programmer can search the cells for data, can arrange the data according to the users need by sorting the contents of the table.

    Usually the user can select only one row at one time. But a user might need to cut, copy or delete one or more than one rows from the table view. For such kind of tasks selecting one row at a time and then performing operation on it is a tedious task. So to make it easy, the user should be able to select more than one row at a time and then perform the function on all of the selected cells at the same time. This is going to save a lot of time and effort of the user.

    One of the methods to do this is to add a check mark on the right side of each cell that is selected to denote that the cell is selected. The code required to perform this is as follows.

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {    
        UITableViewCell *cellCheck = [tableView cellForRowAtIndexPath:indexPath];
        if (cellCheck.accessoryType == UITableViewCellAccessoryNone)
        {
            cellCheck.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else if (cellCheck.accessoryType == UITableViewCellAccessoryCheckmark)
        {
            cellCheck.accessoryType = UITableViewCellAccessoryNone;
        }    
    }
    

    The programmer also have to use an array that keeps track of the cell that are selected by the user in cellForRowAtIndexPath. Just check and verify if the accessory type in the cell is checked or not. If the accessory type is checked assign the row to the new array, if not don't perform any operation.

    NSArray *selectedCellArray = [self.tableview indexpathsForSelectedRows];
    

    The user can unselect the selected row by clicking on the cell again. This is an easy and user friendly method of selecting multiple rows in a UITableView.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: