как применить галочку на табличном представлении в iphone, используя цель c? - PullRequest
4 голосов
/ 05 августа 2009

Я пытаюсь подать заявку на галочку в табличном представлении, но она не работает, если я снова проверил в этой ячейке, тогда галочка применяется. но не применяется в новой выбранной ячейке. кто-нибудь, кто мне поможет ...

спасибо, Аамир.

я использую следующий код

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

Ответы [ 2 ]

8 голосов
/ 05 августа 2009

Вы имеете в виду, что галочка не применяется, когда вы выбираете первую строку в первый раз? Вы можете добавить другое предложение к коду, но оно тратит время на запуск кода.

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;
}
2 голосов
/ 09 мая 2012

Здесь есть забавная ошибка

 if (row != oldRow)

потому что если oldRow был nil (или другими словами ... "0" ;-), и пользователь нажимает строку = "0", то предложение "if" будет считать эту строку проверенной.

Я исправил вот так:

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

Кстати ... не забудьте использовать "я"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...