как проверить uitableviewcell accessoryView содержит изображение или нет - PullRequest
0 голосов
/ 09 июня 2011

Можно ли проверить, uitableviewcell accessoryview содержит изображение или нет?

Например

 if([self.tableView cellForRowAtIndexPath:indexPath].accessoryView == [UIImage  imageNamed:@"selected.png"] )  

Некоторым это нравится или другим способом.
Спасибо

Ответы [ 4 ]

1 голос
/ 09 июня 2011

По умолчанию accessoryView будет нулевым.

Вы можете проверить -

if (self.tableView.accessoryView) {

     // An image is present
}
0 голосов
/ 16 октября 2012

Вот как я могу реализовать многоэлементное табличное представление с флажками в каждой ячейке

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [disclosureButton setFont:[UIFont fontWithName:@"Arial-BoldMT" size:12.0]];
    [disclosureButton setBackgroundImage:[UIImage imageNamed:@"checkboxoff.png"] forState:UIControlStateNormal];
    disclosureButton.tag = 0;
    [disclosureButton setFrame:CGRectMake(0,0, 30,30)];
    [disclosureButton addTarget:self action:@selector(select:event:) forControlEvents:UIControlEventTouchUpInside];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell.textLabel setUserInteractionEnabled:YES];
        [cell setImage:[UIImage imageNamed:@"bluemappointer.png"]];
        [cell setAccessoryView:disclosureButton];
    }
    else{
        int n = indexPath.row;
        if([selectedPlaces objectForKey:indexPath] != nil){
            disclosureButton.tag = 1;
            [disclosureButton setBackgroundImage:[UIImage imageNamed:@"checkboxon.png"] forState:UIControlStateNormal];
            [cell setAccessoryView:disclosureButton];
        }
        else{
            [disclosureButton setBackgroundImage:[UIImage imageNamed:@"checkboxoff.png"] forState:UIControlStateNormal];
            disclosureButton.tag = 0;
            [cell setAccessoryView:disclosureButton];
        }
    }

    NSString *name = [tableData objectAtIndex:indexPath.row];
    [cell.textLabel setText:name];

    return cell;
}
0 голосов
/ 09 июня 2011

Первый:

if([self.tableView cellForRowAtIndexPath:indexPath].accessoryView == [UIImage imageNamed:@"selected.png"] )

это неправильно ... awiseview - это не изображение, это UIView.

Проверить:

if(nil != [self.tableView cellForRowAtIndexPath:indexPath].accessoryView)
{
  // It has got something in it... 
}
else
{
 //create an accessary view....
}
0 голосов
/ 09 июня 2011

Для моего приложения я создал пользовательскую ячейку, а затем добавил imageView в contentView

Но для вашего требования вы можете сделать что-то вроде этого

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        cell.accessoryView = imageView];
        imageView.tag = 3;
        [imageView release];
    }
    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    UIImageView *imageView;
    for (UIView *subview in subviews) 
    {
        if([subview isKindOfClass:[UIImageView class]] && subview.tag == 3);
            imageView = [subview copy];
    }
    [subviews release];

   if([selectedArray containsObject:indexPath])
        imageView.image = [UIImage imageNamed:@"Selected.png"];
    else
        imageView.image = [UIImage imageNamed:@"Unselected.png"];
}

И в этом методе сделатьто же самое, чтобы получить объект imageView

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
...