簡體   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 ");

    }

在模擬器中,當我單擊第一行中的按鈕(indexpath.row = 0)然后滾動表格視圖時,單擊按鈕將自動在第七行中顯示(indexpath.row = 6)

問題是,我想知道,實際上發生了什么以及如何避免這種情況(當我滾動時)?

由於單元已被重用,因此必須(在cellForRowAtIndexPath中)將圖像設置為一種狀態或另一種狀態(以設置一個圖像或另一種狀態)。

真正發生的是,您為一個單元格設置了Image,但是該單元格已在整個表中重用。 您僅使用5個單元格(可以說是一個屏幕可以容納多少個單元格),下次加載時,您實際上是在重復使用一個圖像。

因此,在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;

    }
}

為了完善起見,我還建議您對這些類型的單元格使用Accessory類型以及Xib文件。 您可以在故事板的附件下拉菜單中或通過代碼進行設置:

cell.accessoryType = UITableViewCellAccessoryCheckmark;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM