Как выбрать конкретный флажок в tableView, который вставлен в конструктор интерфейса ячейки таблицы в iphone

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

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

}

введите здесь описание изображения

Спасибо и привет,

жирный


person girish    schedule 24.03.2011    source источник


Ответы (1)


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

Когда вы добавляете его в tableView, вы можете установить для него свойство тега. И тогда вы можете получить тег в методе 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"];

    }
person Vaibhav Tekam    schedule 24.03.2011
comment
Привет, вайбхав, спасибо за ваш ответ, он работает, но у меня есть одна проблема: когда я нажимаю на кнопку, изображение не меняется (галочка). Можете ли вы помочь мне в этом. - person girish; 24.03.2011
comment
это сложно, мы поддерживаем индекс ячейки в теге, поэтому не можем использовать его для выбранного или невыбранного значения. Вы можете установить тег как положительный или отрицательный. Произнесите положительное для невыбранного и отрицательное для выбранного. Это может быть одним из способов справиться с этим. Я отредактирую код для того же. - person Vaibhav Tekam; 24.03.2011
comment
привет, вайбхав, спасибо, все работает.. не могли бы вы переслать мне свой почтовый идентификатор. мой почтовый идентификатор [email protected] - person girish; 24.03.2011