Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableViewCell中的复选框图像切换_Iphone_Cocoa Touch_Uitableview - Fatal编程技术网

Iphone UITableViewCell中的复选框图像切换

Iphone UITableViewCell中的复选框图像切换,iphone,cocoa-touch,uitableview,Iphone,Cocoa Touch,Uitableview,我需要一些关于创建一个左边有一个可以切换的图像的UITableViewCell的指导。该图像应可点击并充当切换(复选框) 我正在努力解决的部分是: 如何检测图像上的点击,并以与didSelectRowAtIndexPath不同的方式处理这些点击 如何在不执行[tableView reloadData]的情况下更改图像 我做了类似的事情(主演了一部最喜欢的电影),但我认为你对iPhone用户的拇指要求很高,因为手机会将人们引导到另一个视角。首先,我要查看单元格上的详细信息披露选项。其中一个选项是可

我需要一些关于创建一个左边有一个可以切换的图像的
UITableViewCell
的指导。该图像应可点击并充当切换(复选框)

我正在努力解决的部分是:

  • 如何检测图像上的点击,并以与
    didSelectRowAtIndexPath
    不同的方式处理这些点击
  • 如何在不执行
    [tableView reloadData]
    的情况下更改图像
  • 我做了类似的事情(主演了一部最喜欢的电影),但我认为你对iPhone用户的拇指要求很高,因为手机会将人们引导到另一个视角。首先,我要查看单元格上的详细信息披露选项。其中一个选项是可以附加到的预制箭头按钮。你的电话


    如果触摸在复选框上,您可能可以捕获didSelectRowAtIndexPath事件,然后执行一些其他逻辑,而不是重定向,尽管我不知道您如何获得该位置。这意味着您可能需要找到一种方法,在touch事件调用didselectrowatindexpath之前找到它,我不太确定该怎么做。你处理过ToucheSbegind之类的东西吗?

    其实很简单

    只需创建一个新的UIControl子类并将其全部放入其中(不需要单独的控制器)。我们将其称为ToggleImageControl

    @interface ToggleImageControl : UIControl
    {
       BOOL selected;
       UIImageView *imageView;
       UIImage *normalImage;
       UIImage *selectedImage;
    }
    
    为每个单元格创建一个ToggleImageControl,并将其添加到适当的位置

    ToggleImageControl *toggleControl = [[ToggleImageControl alloc] initWithFrame: <frame>];
    toggleControl.tag = indexPath.row;  // for reference in notifications.
    [cell.contentView addSubview: toggleControl];
    
    设置UIImageView的image属性以更新图像;这将在没有副作用的情况下触发重新绘制

    - (void) toggleImage
    {
       selected = !selected;
       imageView.image = (selected ? selectedImage : normalImage); 
    
       // Use NSNotification or other method to notify data model about state change.
       // Notification example:
       NSDictionary *dict = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: self.tag forKey: @"CellCheckToggled"];
       [[NSNotificationCenter defaultCenter] postNotificationName: @"CellCheckToggled" object: self userInfo: dict];
    
    }
    
    你显然需要按摩一些东西。您可能希望传入这两个图像名称以使其更具可重用性,而且我建议您也从对象外部指定通知名称字符串(假设您使用的是通知方法)。

    因此“.显然需要按摩一些东西…”注释意味着“…此代码不起作用…”

    所以

    应该是

    - (id)initWithFrame:(CGRect)frame 
    {
     if ( self = [super initWithFrame: frame] ){
      normalImage = [UIImage imageNamed: @"toggleImageNormal.png"];
      selectedImage = [UIImage imageNamed: @"toggleImageSelected.png"];
      imageView = [[UIImageView alloc] initWithImage: normalImage];
    
     // set imageView frame
      [self addSubview: imageView];
    
      [self addTarget: self action: @selector(toggleImage) forControlEvents: UIControlEventTouchDown];
     }
    
     return self;
    }
    

    由于
    -(void)viewDidLoad
    从未被调用。

    如果覆盖touchsbegind,则有一种更简单的方法可以做到这一点:如果不调用,则需要执行if语句以确定它是否在复选标记附近。[super touchsbegind:touchswithevent:event],它会像被选中一样工作。

    这里是“override TouchesBegind:”方法的一个实现,我使用的方法很简单,似乎效果很好

    只需将此类包含在项目中,并在
    tableView:cellforrowatinexpath:
    方法中创建和配置一个
    TouchIconTableViewCell
    而不是
    UITableView
    单元格

    TouchIconTableViewCell.h:

    #import <UIKit/UIKit.h>
    
    @class TouchIconTableViewCell;
    
    @protocol TouchIconTableViewCellDelegate<NSObject>
    
    @required
    - (void)tableViewCellIconTouched:(TouchIconTableViewCell *)cell indexPath:(NSIndexPath *)indexPath;
    
    @end
    
    @interface TouchIconTableViewCell : UITableViewCell {
        id<TouchIconTableViewCellDelegate> touchIconDelegate;       // note: not retained
        NSIndexPath *touchIconIndexPath;
    }
    
    @property (nonatomic, assign) id<TouchIconTableViewCellDelegate> touchIconDelegate;
    @property (nonatomic, retain) NSIndexPath *touchIconIndexPath;
    
    @end
    

    每次创建或重复使用单元格时,请设置
    touchIconDelegate
    touchIconIndexPath
    属性。触摸图标时,将调用代理。然后你可以更新图标或其他任何东西。

    似乎苹果从2011年10月12日开始在iOS开发者程序上上传了“TableMultiSelect”作为示例代码

    此代码可以启用编辑模式下的多项选择

    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    

    尽管它只能从iOS5中使用


    几个小时后,我在Stack Overflow中找不到此示例代码,因此我将此信息添加到此帖子。

    我链接的屏幕截图来自Sophiestation()的Groceries应用程序。我知道这是可能的,因为该应用程序可以做到这一点,而且运行得非常好。我一直在避免创建自己的UITableViewCell类——到目前为止,我没有将视图放在contentView中。要正确实现TouchStart,我想我别无选择,只能创建自己的UITableViewCell类。您是如何编写代码将您的行标记为收藏夹的?这太棒了!谢谢有一个问题,当点击toggleImage控件时,点击将不会冒泡到UITableViewControl的DidSelectRowatingIndexPath。正确吗?不,按钮会截取它并阻止单元格看到它。您也可以将此控件设置为空帧,并使用现有的cell.imageView属性绘制实际图像吗?sehugg:可能。几乎任何事情都可以通过分层控件来完成。当滚动tableview时,会显示图像。它显然不起作用。嗨,雷恩,你怎么能为你的问题制作这张图片?有没有任何软件可用于此?你从哪里获得复选标记图标?我也可以从哪里获得复选标记图标?@雷恩,你是如何创建返回按钮的(左上角)?@London这不是来自我的应用程序-它来自一个名为Groceries的应用程序。对不起,“设置touchIconDelegate和touchIconIndexPath属性”的确切含义是什么?例如://创建单元格TouchIconTableViewCell*单元格=(TouchIconTableViewCell*)[tableView dequeueReusableCellWithIdentifier:Identifier];if(cell==nil)cell=[[[TouchIconTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle重用标识符:标识]自动释放];cell.touchIconDelegate=self;cell.touchIconIndexPath=indexPath;我喜欢。似乎比创建另一个控件来放入我的自定义表单元格要简单一些。这是一些非常好的示例代码。谢谢你提醒我们。
    #import <UIKit/UIKit.h>
    
    @class TouchIconTableViewCell;
    
    @protocol TouchIconTableViewCellDelegate<NSObject>
    
    @required
    - (void)tableViewCellIconTouched:(TouchIconTableViewCell *)cell indexPath:(NSIndexPath *)indexPath;
    
    @end
    
    @interface TouchIconTableViewCell : UITableViewCell {
        id<TouchIconTableViewCellDelegate> touchIconDelegate;       // note: not retained
        NSIndexPath *touchIconIndexPath;
    }
    
    @property (nonatomic, assign) id<TouchIconTableViewCellDelegate> touchIconDelegate;
    @property (nonatomic, retain) NSIndexPath *touchIconIndexPath;
    
    @end
    
    #import "TouchIconTableViewCell.h"
    
    @implementation TouchIconTableViewCell
    
    @synthesize touchIconDelegate;
    @synthesize touchIconIndexPath;
    
    - (void)dealloc {
        [touchIconIndexPath release];
        [super dealloc];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        CGPoint location = [((UITouch *)[touches anyObject]) locationInView:self];
        if (CGRectContainsPoint(self.imageView.frame, location)) {
            [self.touchIconDelegate tableViewCellIconTouched:self indexPath:self.touchIconIndexPath];
            return;
        }
        [super touchesBegan:touches withEvent:event];
    }
    
    @end
    
    self.tableView.allowsMultipleSelectionDuringEditing = YES;