简体   繁体   English

iPhone:UITableView CellAccessory复选标记

[英]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 在iPhone App中单击表格视图单元格我要显示表格视图单元格附件类型在didSelectRowAtIndexPath上的复选标记我正在编写代码

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? 我怎样才能做到这一点?

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 . 如果这是一个NSIndexPath这是最简单的。

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

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

@end

In your cellForRowAtIndexPath add the following: 在您的cellForRowAtIndexPath添加以下内容:

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. 你如何编写tableView:didSelectRowAtIndexPath:将取决于你想要的行为。 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;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *newCell =[tableView cellForRowAtIndexPath:indexPath];
int newRow = [indexPath row];
int oldRow = [lastIndexPath row];
if (newRow != oldRow)
{
    newCell = [tableView  cellForRowAtIndexPath:indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;
    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; 
    oldCell.accessoryType = UITableViewCellAccessoryNone;
    lastIndexPath = indexPath;  
}
}

For reference take a look at this discussion 有关参考,请参阅此讨论

this is a simple solution that worked for me: 这是一个对我有用的简单解决方案:

in your .h file declare: 在.h文件中声明:

NSIndexPath *checkedCell;

then in your .m file add: 然后在你的.m文件中添加:

        if (checkedCell == indexPath) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
        } else { 
cell.accessoryType = UITableViewCellAccessoryNone; 
}

to the cellForRowAtIndexPath method and cellForRowAtIndexPath方法和

checkedCell = indexPath;
[tableView reloadData]

to the didSelectRowAtIndexPath method. didSelectRowAtIndexPath方法。

Best of luck! 祝你好运!

I worked on your problem and got a solution 我解决了你的问题并找到了解决方案

in .h file 在.h文件中

int rowNO;
    NSIndexPath *lastIndexPth;

in . 在。 m file m文件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%d",indexPath.row);
    if (rowNO!=indexPath.row) {
        rowNO=indexPath.row;        
        [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;
        [tableView cellForRowAtIndexPath:lastIndexPth].accessoryType=UITableViewCellAccessoryNone;
        lastIndexPth=indexPath;
    }

I hope this might help you... 我希望这可以帮助你......

its simple 这很简单

in .h 在.h

NSIndexPath   checkedCell;

In .m 在.m

cellForRowAtIndexPath


if ([checkedCell isEqual:indexPath]) 

{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;


} else
{ 
    cell.accessoryType = UITableViewCellAccessoryNone; 
}

in didSelectRowAtIndexPath 在didSelectRowAtIndexPath中

checkedCell = indexPath;

[tableView reloadData]

I hope this one solves your problem : 我希望这个解决你的问题:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

int newRow = [indexPath row];
int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;

if (newRow != oldRow)
{
    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                indexPath];
    newCell.accessoryType = UITableViewCellAccessoryCheckmark;

    UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: 
                                lastIndexPath]; 
    oldCell.accessoryType = UITableViewCellAccessoryNone;
    lastIndexPath = indexPath;
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

You could store NSIndexPath of the checked row as the class variable, whenever you try to the others row just access the previously checked row using the NSIndexPath variable stored as class variable and unchecked that. 您可以将已检查行的NSIndexPath存储为类变量,每当您尝试其他行时,只需使用存储为类变量的NSIndexPath变量访问先前检查的行并取消选中该行。

Check the below post 检查以下帖子

http://www.iphonedevsdk.com/forum/iphone-sdk-development/24240-uitableviewcellaccessorycheckmark-one-checkmark-time.html http://www.iphonedevsdk.com/forum/iphone-sdk-development/24240-uitableviewcellaccessorycheckmark-one-checkmark-time.html

Add to your UITableViewDataSource delegate variable NSIndexPath *checkedIndex . 添加到UITableViewDataSource委托变量NSIndexPath *checkedIndex Add to didSelectRowAtIndexPath: 添加到didSelectRowAtIndexPath:

checkedIndex = indexPath;
[tableView reload];

And change your cellForRowAtIndexPath: method: 并更改你的cellForRowAtIndexPath:方法:

cell = .. // init or dequeue
if ([indexPath isEqualTo:checkedIndex])
{
    cell.accessoryType=UITableViewCellAccessoryCheckmark;
}
else
{
    cell.accessoryType=UITableViewCellAccessoryNone;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM