簡體   English   中英

滾動UITableView時如何顯示選中的圖像?

[英]How to display checked images while scrolling the UITableView?

UITable視圖工作正常。 當滾動選中的圖像更改為取消選中時。 而且我還需要在“保存”按鈕Action中獲取特定的已檢查圖像數據。任何機構都可以幫助我解決prj中的此問題。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.backgroundColor = [UIColor clearColor];

        cell.selectionStyle= UITableViewCellSelectionStyleNone;
    }

        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
       [btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];        [cell addSubview:btn];
        btn.tag=4;
        forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];



        UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
        lbl_name.text=[NSString stringWithFormat:@"%@",[[arr1 valueForKey:@"Name"]objectAtIndex:indexPath.row]];
        lbl_name.tag=5;
        lbl_name.textColor=[UIColor blackColor];
        [lbl_name setTextAlignment:NSTextAlignmentCenter];
        lbl_name.font=[UIFont systemFontOfSize:15];
        [cell addSubview:lbl_name];

    return cell;

}

-(void)checkBoxClicked:(id)sender
{
    UIButton *tappedButton = (UIButton*)sender;
    if([tappedButton.currentImage isEqual:[UIImage imageNamed:@"unchecked.png"]])
    {
        [sender  setImage:[UIImage imageNamed: @"checked.png"] forState:UIControlStateNormal];
    } else {
        [sender setImage:[UIImage imageNamed:@"unchecked.png"]forState:UIControlStateNormal];
    }
}

您可以使用不同的狀態來設置圖像。

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.backgroundColor = [UIColor clearColor];

        cell.selectionStyle= UITableViewCellSelectionStyleNone;
    }

        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];
       [btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];  
        [sender  setImage:[UIImage imageNamed: @"checked.png"] forState:UIControlStateSelected]; 
        btn.selected=NO;  
        btn.tag=4;
        [btn addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];

        UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
        lbl_name.text=[NSString stringWithFormat:@"%@",[[arr1 valueForKey:@"Name"]objectAtIndex:indexPath.row]];
        lbl_name.tag=5;
        lbl_name.textColor=[UIColor blackColor];
        [lbl_name setTextAlignment:NSTextAlignmentCenter];
        lbl_name.font=[UIFont systemFontOfSize:15];
        [cell addSubview:lbl_name];

    return cell;

}

-(void)checkBoxClicked:(id)sender
{
    UIButton *tappedButton = (UIButton*)sender;
     if (tappedButton.isSelected) {
    tappedButton.selected=NO;
}
else
{
    tappedButton.selected=YES;
}
}

取一個數組,該數組用於存儲Selected按鈕的IndexValue,

@property (nonatomic,retain) NSMutableArray *arySelected;

在ViewDidLoad中初始化數組,

- (void)viewDidLoad {

    [super viewDidLoad]; 
    self.arySelected = [[NSMutableArray alloc] init];

}

更改TableViewCell委托的代碼,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if(cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
            cell.backgroundColor = [UIColor clearColor];

            cell.selectionStyle= UITableViewCellSelectionStyleNone;
        }

        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(10,10, 20, 20)];

        if (![arySelected containsObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]])
        {
            [btn  setImage:[UIImage imageNamed: @"checked.png"] forState:UIControlStateSelected];

        }
        else
        {
            [btn setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];

        }

        btn.tag=indexPath.row;
        [btn addTarget:self action:@selector(checkBoxClicked:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];

        UILabel *lbl_name =[[UILabel alloc]initWithFrame:CGRectMake(35, 10, 100, 20)];
        lbl_name.text=[NSString stringWithFormat:@"%@",[[arr1 valueForKey:@"Name"]objectAtIndex:indexPath.row]];
        lbl_name.tag=5;
        lbl_name.textColor=[UIColor blackColor];
        [lbl_name setTextAlignment:NSTextAlignmentCenter];
        lbl_name.font=[UIFont systemFontOfSize:15];
        [cell addSubview:lbl_name];

        return cell;
}

//在事件按鈕上,

-(void)checkBoxClicked:(id)sender
{
    sender.selected  = ! sender.selected;

    if (sender.selected)
    {
        [arySelected addObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
    }
    else
    {
        [arySelected removeObject:[NSString stringWithFormat:@"%ld",(long)sender.tag]];
    }


    [self.tableView reloadData];
}

暫無
暫無

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

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