开发者

how to check uitableviewcell accessoryView contains image or not

Is it possible to check uitableviewcell accessoryview contains image or not?

For example

 if([self.tableView cellForRowAtIndexPath:indexPath].accessoryView == [UIImage  imageNamed:@"selected.p开发者_开发百科ng"] )  

Some this like that or with different method.

Thank you


By default accessoryView will be null.

You can check for -

if (self.tableView.accessoryView) {

     // An image is present
}


For my app I have created a custom cell and then added the imageView at the contentView

But for your requirement you can do something like this

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

And in this method do the same thing to get the imageView object

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


First:

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

this is wrong... acessaryview is not an image, it's UIView.

Check for:

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


This is how I implement multiselect tableview with checkboxes in each cell

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜