简体   繁体   English

uitableview滚动时,如何停止其他单元格中的显示按钮单击(更改按钮图像)(单击的单元格图像已更改)?

[英]how to stop the display button click (change button image) in other cell (clicked cell image is changed ) when uitableview scroll?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 30;    //count number of row from counting array hear cataGorry is An Array
}



- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *testword=@"pass";
    if ([testword isEqualToString:@"pass"]) {
        static NSString *MyIdentifier = @"cell1";
        TextTableViewCell *cell ;




        cell= [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        cell.lblText.text=[NSString stringWithFormat:@"textcelll= %i", indexPath.row+1];


        cell.btnTextbox.tag=indexPath.row;
        [cell.btnTextbox addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];



        return cell;

    }else{

    static NSString *MyIdentifier1 = @"cell2";

    ImageTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:MyIdentifier1];
        return cell1;

    }
}
-(void)customActionPressed:(UIButton*)sender{

    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView1];
    NSIndexPath *indexPath = [self.tableView1 indexPathForRowAtPoint:buttonPosition];

    TextTableViewCell *cell = (TextTableViewCell*)[self.tableView1 cellForRowAtIndexPath:indexPath];

    if (indexPath != nil)
    {
        int currentIndex = indexPath.row;
        NSLog(@"currentIndex == %d",currentIndex);
        int tableSection = indexPath.section;
        NSLog(@"tableSection == %d",tableSection);
    }
    if (!cell.btnTextbox.isSelected ){
        cell.btnTextbox.selected=YES;
        [cell.btnTextbox setImage:[UIImage imageNamed:@"checkClick.png"] forState:UIControlStateNormal];
        NSLog(@"button tag %i",cell.btnTextbox.tag);
        NSLog(@"check click");

    }else{
        cell.btnTextbox.selected=NO;
        [cell.btnTextbox setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
        NSLog(@"check ");

    }

in simulator , when i am clicked the button in first row (indexpath.row=0) then i am scrolling tableview, button click will auto display in 7th row (indexpath.row=6) 在模拟器中,当我单击第一行中的按钮(indexpath.row = 0)然后滚动表格视图时,单击按钮将自动在第七行中显示(indexpath.row = 6)

Question is ,i want to know , what happened in really and how to avoid this (when i'm scrolling)? 问题是,我想知道,实际上发生了什么以及如何避免这种情况(当我滚动时)?

Since cells are being reused, you have to (in your cellForRowAtIndexPath) to set image to one state or another (to set one image or another). 由于单元已被重用,因此必须(在cellForRowAtIndexPath中)将图像设置为一种状态或另一种状态(以设置一个图像或另一种状态)。

What really happen is that you set Image for one cell but that cell is being reused through whole table. 真正发生的是,您为一个单元格设置了Image,但是该单元格已在整个表中重用。 You use only 5(lets say that is how much you cells can fit into one screen) cells and when you load next you actually reuse the one with an image. 您仅使用5个单元格(可以说是一个屏幕可以容纳多少个单元格),下次加载时,您实际上是在重复使用一个图像。

So, in your cellForRowAtIndexPath you will have to check if the button state is selected or not and then assign appropriate image. 因此,在cellForRowAtIndexPath中,您将必须检查是否选择了按钮状态,然后分配适当的图像。

- (UITableViewCell *)tableView:(UITableView *)tableView
             cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *testword=@"pass";
    if ([testword isEqualToString:@"pass"]) {
        static NSString *MyIdentifier = @"cell1";
        TextTableViewCell *cell ;




        cell= [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
        cell.lblText.text=[NSString stringWithFormat:@"textcelll= %i", indexPath.row+1];


        cell.btnTextbox.tag=indexPath.row;
        if (cell.btnTextbox.isSelected ){
          [cell.btnTextbox setImage:[UIImage imageNamed:@"checkClick.png"]        forState:UIControlStateNormal];


        }else{
        [cell.btnTextbox setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];


    }   

        [cell.btnTextbox addTarget:self action:@selector(customActionPressed:) forControlEvents:UIControlEventTouchUpInside];



        return cell;

    }else{

    static NSString *MyIdentifier1 = @"cell2";

    ImageTableViewCell *cell1 = [tableView dequeueReusableCellWithIdentifier:MyIdentifier1];
        return cell1;

    }
}

For completness sake, I would also suggest you to use Accessory type as well as Xib files for these kind of cells. 为了完善起见,我还建议您对这些类型的单元格使用Accessory类型以及Xib文件。 You can set it in storyboard under accessory dropdown or through code: 您可以在故事板的附件下拉菜单中或通过代码进行设置:

cell.accessoryType = UITableViewCellAccessoryCheckmark;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM