如何检查 uitableviewcellaccessoryView 是否包含图像

发布于 2024-11-15 01:03:01 字数 255 浏览 4 评论 0原文

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

例如,

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

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

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

For example

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

Some this like that or with different method.
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蘸点软妹酱 2024-11-22 01:03:01

默认情况下,accessoryView 将为 null。

您可以检查 -

if (self.tableView.accessoryView) {

     // An image is present
}

By default accessoryView will be null.

You can check for -

if (self.tableView.accessoryView) {

     // An image is present
}
鹿! 2024-11-22 01:03:01

对于我的应用程序,我创建了一个自定义单元格,然后在 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

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
定格我的天空 2024-11-22 01:03:01

首先:

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....
}

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....
}
情话已封尘 2024-11-22 01:03:01

这就是我如何在每个单元格中实现带有复选框的多选表格视图

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文