Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

objective c - iPhone :UITableView CellAccessory Checkmark

In iPhone App on click of table view cell I want to display Table view cell Accessory type Check mark for that on didSelectRowAtIndexPath i am writing code

if(indexPath.row ==0)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
 }

and displaying check marks.

but in my case i wnt to allow user to check only on cell at a time means if user select other row then that row should checked and previously checked should be unchecked

How can I achieve that?

question from:https://stackoverflow.com/questions/5959950/iphone-uitableview-cellaccessory-checkmark

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Keep track, in an instance variable, of which row is checked. When the user selects a new row, first uncheck the previously checked row, then check the new row and update the instance variable.

Here is more detail. First add a property to keep track of the currently checked row. It's easiest if this is an NSIndexPath.

@interface RootViewController : UITableViewController {
    ...
    NSIndexPath* checkedIndexPath;
    ...
}

...
@property (nonatomic, retain) NSIndexPath* checkedIndexPath;
...

@end

In your cellForRowAtIndexPath add the following:

if([self.checkedIndexPath isEqual:indexPath])
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else 
{
    cell.accessoryType = UITableViewCellAccessoryNone;
}

How you code your tableView:didSelectRowAtIndexPath: will depend on the behavior you want. If there always must be a row checked, that is, that if the user clicks on an already checked row use the following:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                     cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    self.checkedIndexPath = indexPath;
}

If you want to allow the user to be able to uncheck the row by clicking on it again use this code:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Uncheck the previous checked row
    if(self.checkedIndexPath)
    {
        UITableViewCell* uncheckCell = [tableView
                    cellForRowAtIndexPath:self.checkedIndexPath];
        uncheckCell.accessoryType = UITableViewCellAccessoryNone;
    }
    if([self.checkedIndexPath isEqual:indexPath])
    {
        self.checkedIndexPath = nil;
    }
    else
    {
        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        self.checkedIndexPath = indexPath;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...