iPhone:UITableView CellAccessory Checkmark [英] iPhone :UITableView CellAccessory Checkmark
问题描述
在iPhone应用程序中单击表格视图单元格我想显示表格视图单元格附件类型在didSelectRowAtIndexPath上的复选标记我正在编写代码
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;
}
并显示复选标记。
但在我的情况下,我想允许用户一次只检查单元格意味着用户是否选择了其他行然后应检查该行并且之前检查应该取消选中
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.
这里更详细。首先添加一个属性以跟踪当前检查的行。如果这是 NSIndexPath
,这是最简单的。
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
在你的 cellForRowAtIndexPath
添加以下内容:
if([self.checkedIndexPath isEqual:indexPath])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
如何编码 tableView:didSelectRowAtIndexPath:
将取决于您想要的行为。
如果必须检查行,即如果用户点击已经检查过的行,请使用以下内容:
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;
}
}
这篇关于iPhone:UITableView CellAccessory Checkmark的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!