Как установить определенный флажок в tableView, который вставляется в конструктор интерфейса ячейки таблицы в iphone - PullRequest
0 голосов
/ 24 марта 2011

Я делаю небольшую концепцию на флажок.На самом деле, я поместил изображение флажка в ячейку таблицы. Но я не могу понять, как выбрать конкретный флажок и получить значение ячейки в табличном представлении. Может ли какое-либо тело помочь мне решить эту проблему?предоставить скриншоты представления таблицы с кодировкой,

- (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"];

}

enter image description here

Спасибо и всего наилучшего,

Гириш

1 Ответ

2 голосов
/ 24 марта 2011

Вы добавляете кнопку в файл RequestSongSelectingCell.xib?

Когда вы добавляете его в tableView, вы можете установить его свойство tag. И тогда вы можете получить тег в методе buttonPressed.

- (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"];

}

РЕДАКТИРОВАТЬ: для обработки выбора

- (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"];

    }
...