如何检查uitableviewcell accessoryView是否包含图像

时间:2011-06-09 10:29:38

标签: iphone uitableview uiimage accessoryview

是否可以检查uitableviewcell accessoryview是否包含图片?

例如

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

有些像这样或用不同的方法 谢谢

4 个答案:

答案 0 :(得分:1)

默认情况下,accessoryView将为null。

您可以查看 -

if (self.tableView.accessoryView) {

     // An image is present
}

答案 1 :(得分:0)

对于我的应用,我创建了一个自定义单元格,然后在contentView

添加了imageView

但是根据您的要求,您可以做类似的事情

- (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

答案 2 :(得分:0)

第一:

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

这是错误的... acessaryview不是图像,它是UIView。

检查:

if(nil != [self.tableView cellForRowAtIndexPath:indexPath].accessoryView)
{
  // It has got something in it... 
}
else
{
 //create an accessary view....
}

答案 3 :(得分:0)

这就是我在每个单元格中使用复选框实现多选表格视图的方法

- (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;
}