Настройка ячейки просмотра таблицы

#iphone

#iPhone

Вопрос:

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

Это мой код:

iPhone

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"Cell";
    BOOL checkButtonPressed=FALSE;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    UIImage *image = (checkButtonPressed) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"];

    UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    checkButton.frame = frame;  // match the button's size with the image size

    [checkButton setBackgroundImage:image forState:UIControlStateNormal];



    [checkButton addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
    //cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
    cell.textLabel.adjustsFontSizeToFitWidth = YES;


    for (int ii=0; ii<=[checkedArr count]; ii  ) {
        cell.textLabel.text =[dataArray objectAtIndex:ii];
    }

    //cell.backgroundColor = [UIColor clearColor];
    cell.accessoryView = checkButton;
    [cellArr addObject:cell];
    [checkedArr addObject:[NSNumber numberWithBool:FALSE]];

    return cell;



}

- (void)checkButtonTapped:(id)sender event:(id)event
{

    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:tableView];
    NSIndexPath *indexPath = [tableView indexPathForRowAtPoint: currentTouchPosition];
    NSLog(@"val %d",[checkedArr count]);
    if (indexPath != nil amp;amp; iChecked == 0)
    {
        [self tableView:tableView accessoryButtonTappedForRowWithIndexPath: indexPath];
    }
}


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{   
    NSLog(@"in accessoryButtonTappedForRowWithIndexPath %d",indexPath.row);

    BOOL checked = [[checkedArr objectAtIndex:indexPath.row] boolValue];


    [checkedArr replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:!checked]];

    UITableViewCell *cell = [cellArr objectAtIndex:indexPath.row];
    UIButton *button = (UIButton *)cell.accessoryView;

    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"];
    [button setBackgroundImage:newImage forState:UIControlStateNormal];
    cell.accessoryView = button;
    iChecked = 0;



}
  

Комментарии:

1. Что ж, я поддерживаю приведенный выше комментарий.

2. Согласен, маловероятно, что вы получите какую-либо помощь сейчас…

Ответ №1:

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

 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if (curSelRowIndex == indexPath.row) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;}
else {
cell.accessoryType = UITableViewCellAccessoryNone;}
return cell;
}

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
curSelRowIndex = indexPath.row;
[tblHistory reloadData];
}