UITableView changes image to cell selection and reset others

I want to change UIImagewhich is inside UITableViewCellwhen the user selects a row. That is now too much. I can do it in didSelectRowAtIndexjust fine. However, the problem is that I want the other cells (each to cellhave a standard image) to have a standard image again. (If the cells again have a standard image, the selected image should be “marked” (this means that the unlabeled image will be replaced by the marked image, for which I already have a code)

Pay attention to Checkboxwhich you know from HTML.

Here is my attempt:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView cellForRowAtIndexPath:indexPath];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];

    //Little flashing animation
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [UIView animateWithDuration:2.0f animations:^{

    } completion:^(BOOL finished) {
        ;
    }];

    //Mark the selected cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ALog(@"%i", cell.imageView.tag);
    ALog(@"%i", [[[tableView cellForRowAtIndexPath:indexPath] imageView] tag]);

    UIImageView *iconimgView = (UIImageView *)[[tableView cellForRowAtIndexPath:indexPath] viewWithTag:TAG_IMAGEMARKERCELL];
    [iconimgView setImage:[UIImage imageNamed:@"circle-answer-marked.png"]];

    //..
}

Here is defined UIImageView:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        //Add this to cell only once, not everytime a cell is displayed! (Everyhing goes in here)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        [[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
        [[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
        [[cell textLabel] setFont:[UIFont fontWithName:@"PTSans-Regular" size:33 / 2]];
        [[cell textLabel] setTextColor:[self colorFromHexString:COLOR_DEFAULT_TEXT_COLOR]];
        cell.indentationLevel = cell.indentationLevel + 2;

        //        //Marker image
        imgCell_Marker = [[UIImageView alloc]initWithFrame:CGRectMake(10, 22, 20, 20)];
        imgCell_Marker.image=[UIImage imageNamed:@"circle-answer.png"];
        imgCell_Marker.tag = TAG_IMAGEMARKERCELL;
        ALog(@"%@", imgCell_Marker);
        [cell.contentView addSubview:imgCell_Marker];
    }

        //..
        return cell;
}

, UIImageView tag, , didSelectRowAtIndexPath "0". ( tag)

.

+4
5

.

-, , , , . , , , , . , . , . cellForRowAtIndexPath , .

-, tag - imgCell_Marker imageView. :

ALog(@"%i", [[[tableView cellForRowAtIndexPath:indexPath] imageView] tag]);

imageView, tag . viewWithTag:TAG_IMAGEMARKERCELL .

0

:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    [tableView reloadData];
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    cell.imageView.image = [UIImage imageNamed:@"image.png"];
}

, .

+7

"Check Mark" Cell. . - . , ,

0

. UITableViewCell:: selectedBackgroundView, .

0

: , , , ( , , , )

:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    [[cell textLabel] setNumberOfLines:0]; // unlimited number of lines
    [[cell textLabel] setLineBreakMode:NSLineBreakByWordWrapping];
    [[cell textLabel] setFont:[UIFont fontWithName:@"PTSans-Regular" size:33 / 2]];
    [[cell textLabel] setTextColor:[self colorFromHexString:COLOR_DEFAULT_TEXT_COLOR]];
    cell.indentationLevel = cell.indentationLevel + 2;

    //Marker image
    imgCell_Marker = [[UIImageView alloc]initWithFrame:CGRectMake(10, 22, 20, 20)];
    imgCell_Marker.image=[UIImage imageNamed:@"circle-answer.png"];
    imgCell_Marker.tag = TAG_IMAGEMARKERCELL;
    [cell.contentView addSubview:imgCell_Marker];

    //.. (more data here, just snipped it)
    return cell;
}

:

I know that my array from which the data for comes from UITableViewis located only in the top 5 points. So I can just remove the if statement in my method, cellForRowAtIndexPathmy UITableView. This will not hurt my application (or wasting CPU time) because there are so few things that no one knows.

However, this solution is pretty ugly. But everything is fine (I think) in this case

0
source

Source: https://habr.com/ru/post/1524726/


All Articles