如何使用目标c在iPhone的表格视图上应用复选标记? [英] how to apply check mark on table view in iphone using objective c?

查看:71
本文介绍了如何使用目标c在iPhone的表格视图上应用复选标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表格视图中申请复选标记,但是它不起作用,如果我再次在该单元格中进行了复选,则复选标记适用.但不适用于新选择的单元格. 那里有谁能帮助我....

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....

感谢阿米尔.

我使用以下代码

#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];
}

推荐答案

您是说第一次选择第一行时,复选标记不适用吗?您可以在代码中添加else语句,但是在运行代码时会浪费一些时间.

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.

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;
}

这篇关于如何使用目标c在iPhone的表格视图上应用复选标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆