objective-c 如何使用目标 c 在 iphone 中的表格视图上应用复选标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1231427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to apply check mark on table view in iphone using objective c?
提问by Aamir
i m trying for apply for check mark in table view, but it is not working, if i checked again at that cell again then check mark apply. but not apply at new selected cell. any one there who help me....
我正在尝试在表格视图中申请复选标记,但它不起作用,如果我再次检查该单元格,则应用复选标记。但不适用于新选定的单元格。有谁帮助我....
thanks aamir.
谢谢阿米尔。
i m using following code
我使用以下代码
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger) tableView:(UITableView *) tableView numberOfRowsInSection:(NSInteger) section
{
return [self.chaptersList count];
}
-(UITableViewCell *) tableView:(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath
{
static NSString *CheckMarkCellIdentifier = @"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];
if ( cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CheckMarkCellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
cell.textLabel.text = [chaptersList objectAtIndex:row];
cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
cell.accessoryType = (row == oldRow && lastIndexPath != nil) ?
UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
int row = [indexPath row];
int oldRow = [lastIndexPath row];
if (row != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
回答by Chilly Zhong
Do you mean that the checkmark doesn't apply when you choose the first line at the first time? You can add an else sentence to the code, but it wastes some time when running the code.
您的意思是第一次选择第一行时,复选标记不适用吗?可以在代码中添加else语句,但是在运行代码时会浪费一些时间。
if (newRow != oldRow)
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
else
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
lastIndexPath = indexPath;
}
回答by RubenVot
There is a funny bug here
这里有一个有趣的错误
if (row != oldRow)
because if oldRow was nil (or in another words... "0" ;-) and the user press the row = "0" the "if" sentence will think this row was checked.
因为如果 oldRow 为零(或者换句话说...“0”;-)并且用户按下行 =“0”,则“if”语句会认为该行已被检查。
I fixed like this:
我是这样固定的:
- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{
if (!self.lastIndexPath) {
self.lastIndexPath = indexPath;
}
if ([self.lastIndexPath row] != [indexPath row])
{
UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:self.lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
self.lastIndexPath = indexPath;
}
else {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath: indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
By the way... dont forget to use "self"
顺便说一句......不要忘记使用“自我”

