How to Select particular check Box in tableView which is inserted in table cell interface builder in iphone

筅森魡賤 提交于 2019-12-31 06:13:53

问题


I am doing a small concept on check box. Actually, i placed the check box image in the table cell.But i can't understand, how to select the particular check box and get the value of the cell in table view, Can any body help me by solving this problem.I will provide the screen shots of table view with coding,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
     NSLog(@"in table view cell for row at index");

     RequestSongSelectingCell *cell = (RequestSongSelectingCell *) [tableView dequeueReusableCellWithIdentifier:@"requestsingcell"];
    if (cell==nil) 
    {

        [[NSBundle mainBundle] loadNibNamed:@"RequestSongSelectingCell" owner:self options:nil];
        NSLog(@"start");
        cell=requestingCell;
        NSLog(@"end");
    }

    NSDictionary *dict=[self.array objectAtIndex:indexPath.row];

    NSString *artistname=[dict objectForKey:@"artist"];
    NSLog(@"artistname is %@",artistname);

    cell.artistName.text=artistname;

    NSString *songtitle=[dict objectForKey:@"title"];
    NSLog(@"songtitle is %@",songtitle);
    cell.artistTitle.text=songtitle;

    return cell;
 }

-(IBAction)checkButtonPressed:(id)sender
{
    NSLog(@"check box button pressed");

    requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];

}

Thanks and regards,

girish


回答1:


Are you adding a button on the RequestSongSelectingCell.xib file?

When you add it on the tableView, you can set the tag property of it. And then you can retrieve tag in the buttonPressed method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSLog(@"in table view cell for row at index");

   RequestSongSelectingCell *cell = (RequestSongSelectingCell *) [tableView dequeueReusableCellWithIdentifier:@"requestsingcell"];
        if (cell==nil) 
    {

        [[NSBundle mainBundle] loadNibNamed:@"RequestSongSelectingCell" owner:self options:nil];
                NSLog(@"start");
        cell=requestingCell;
        NSLog(@"end");
    }
    cell.btn.tag = indexPath.row;
    // your customization
    return cell; 
}

-(IBAction)checkButtonPressed:(id)sender
{
    UIButton * btn = (UIButton*)sender;
    int selected_index = btn.tag;    // This is your index of selected cell

    NSLog(@"check box button pressed");

    requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];

}

EDIT: To handle selection

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSLog(@"in table view cell for row at index");

   RequestSongSelectingCell *cell = (RequestSongSelectingCell *) [tableView dequeueReusableCellWithIdentifier:@"requestsingcell"];
        if (cell==nil) 
    {

        [[NSBundle mainBundle] loadNibNamed:@"RequestSongSelectingCell" owner:self options:nil];
                NSLog(@"start");
        cell=requestingCell;
        NSLog(@"end");
    }
//check if cell is already selected or not
        // if not selected, tag will be positive
        cell.btn.tag = indexPath.row;
        // if selected, tag will be negative
        cell.btn.tag = -indexPath.row;
        // your customization
        return cell; 
    }

-(IBAction)checkButtonPressed:(id)sender
{


    UIButton * btn = (UIButton*)sender;
    int selected_index = btn.tag;    // This is your index of selected cell


    //btn is the same btn image of which you need to change/toggle
    if(btn.tag > 0)     //its not selected
    {
            //set btn image as selected
            btn.tag = -(btn.tag);
            [btn setImage: forState:UIControlStateNormal];
    }
    else if(btn.tag < 0)     //its selected
    {
            //set btn image as unselected
            btn.tag = -(btn.tag);
            [btn setImage: forState:UIControlStateNormal];
    }
    NSLog(@"check box button pressed");

//        requestingCell.checkBoxButton.imageView.image=[UIImage imageNamed:@"checkbox-checked.png"];

    }


来源:https://stackoverflow.com/questions/5415732/how-to-select-particular-check-box-in-tableview-which-is-inserted-in-table-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!