为什么在点击(ios)时我的自定义附件按钮没有更改图像?

杰斯基

我正在尝试使附件视图类似于Apple的演示,但我一定错过了一些东西,因为单击时我的图像不会切换。它只是闪烁

不知道我错过了什么,我试图将其完全基于演示代码。我不得不将几个字典更改为可变的,并添加了if单元格空子句以使我的工作正常。
项目已获取并正确显示在表格视图中。
我鼓舞了我所改变的部分,我想我都明白了。

任何帮助,非常感谢。

我的代码:

标头声明:

@interface PollQueryController : UIViewController <UITableViewDataSource, UITableViewDelegate>

.m文件:




    @interface PollQueryController ()

        @property (nonatomic, strong) NSMutableArray *dataArray;

    @end

    @implementation PollQueryController


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization

        }
        return self;
    }

    - (void)viewDidLoad
    {

        [super viewDidLoad];

    //    NSString *path = [[NSBundle mainBundle] pathForResource:@"pollData" ofType:@"plist"];
    //    NSDictionary *pollsDictionary = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"pollData" ofType:@"plist"]];
        
        NSArray * pollsData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pollData" ofType:@"plist"]];
        NSMutableArray * choices = [[[pollsData objectAtIndex:0] objectForKey:@"choices"] mutableCopy]; // mutable

    //    NSMutableArray *choices = [NSMutableArray arrayWithArray:choicesArray]; // Mutable

        self.dataArray = choices;

        NSLog(@"array: %@", self.dataArray);

    // swipe gestures
        UISwipeGestureRecognizer * Swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
        Swiperight.direction=UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:Swiperight];

        UISwipeGestureRecognizer * Swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
        Swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
        [self.view addGestureRecognizer:Swipeleft];

        self.title = @"Poll Query";

        self.feedTableView.dataSource = self;
        self.feedTableView.delegate = self;

        self.feedTableView.backgroundColor = [UIColor whiteColor];
        self.feedTableView.separatorColor = [UIColor colorWithWhite:0.9 alpha:0.6];


        UIColor* mainColor = [UIColor colorWithRed:50.0/255 green:102.0/255 blue:147.0/255 alpha:1.0f];

        NSString* fontName = @"Optima-Italic";
        NSString* boldFontName = @"Optima-ExtraBlack";

        UIFont* socialFont = [UIFont fontWithName:boldFontName size:10.0f];
        UIColor* socialColor = [UIColor lightGrayColor];

    }



    #pragma mark - UITableViewDataSource


    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return self.dataArray.count;
    }


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
        (NSIndexPath *)indexPath{

        static NSString *kCustomCellID = @"MyCellID";

        FeedCell3* cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];

    // i had to add this, whereas the demo doesnt do this.

           if (cell == nil) {
               cell = [[FeedCell3 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"feedcell3"];
           }


        NSMutableDictionary *item = [[self.dataArray objectAtIndex:indexPath.row] mutableCopy];   // mutable
        cell.textLabel.text = [item objectForKey:@"text"];


        [item setObject:cell forKey:@"cell"];

        BOOL checked = [[item objectForKey:@"checked"] boolValue];

        UIImage *image = (checked) ? [UIImage imageNamed:@"ticked24"] : [UIImage imageNamed:@"unticked24"];

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
        button.frame = frame;
        [button setBackgroundImage:image forState:UIControlStateNormal];

        [button addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor clearColor];
        cell.accessoryView = button;

        UIColor* blueColor = [UIColor colorWithRed:47.0/255 green:168.0/255 blue:228.0/255 alpha:1.0f];


        cell.textLabel.textColor = blueColor;
        NSString* fontName = @"Optima-Italic";
        NSString* boldFontName = @"Optima-ExtraBlack";
        UIFont* socialFont = [UIFont fontWithName:boldFontName size:10.0f];
        cell.textLabel.font = socialFont;
        cell.textLabel.textAlignment = UITextAlignmentCenter;


        return cell;
    }


    #pragma mark - UITableViewDelegate


    - (void)checkButtonTapped:(id)sender event:(id)event
    {
        NSSet *touches = [event allTouches];
        UITouch *touch = [touches anyObject];
        CGPoint currentTouchPosition = [touch locationInView:self.feedTableView];
        NSIndexPath *indexPath = [self.feedTableView indexPathForRowAtPoint: currentTouchPosition];

        if (indexPath != nil)
        {
            [self tableView: self.feedTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
        }
    }

    - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
    {
        NSMutableDictionary *item = [[self.dataArray objectAtIndex:indexPath.row] mutableCopy] ; // mutable

        BOOL checked = [[item objectForKey:@"checked"] boolValue];

        [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"];

        UITableViewCell *cell = [item objectForKey:@"cell"];
        UIButton *button = (UIButton *)cell.accessoryView;

        UIImage *newImage = (checked) ? [UIImage imageNamed:@"unticked24"] : [UIImage imageNamed:@"ticked24"];
        [button setBackgroundImage:newImage forState:UIControlStateNormal];
    }

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

        [self tableView: self.feedTableView accessoryButtonTappedForRowWithIndexPath: indexPath];
        [self.feedTableView deselectRowAtIndexPath:indexPath animated:YES];
    }



    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }


    -(void)swipeleft:(UISwipeGestureRecognizer*)gestureRecognizer
    {

        if(_hasVoted){

        [self performSegueWithIdentifier: @"QueryToResults" sender: self];
        }

    }

    -(void)swiperight:(UISwipeGestureRecognizer*)gestureRecognizer
    {

        [self performSegueWithIdentifier: @"friendsBackToPollSeg" sender: self];

    }



    @end

杰斯基

哇。我发现了为什么不使用mutableCopy而不为我工作的原因。
原始数组不可

NSArray * pollsData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pollData" ofType:@"plist"]];

更改更改为可变可解决此问题。

NSMutableArray * pollsData = [NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pollData" ofType:@"plist"]];

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的JFrame图像没有更改图标?

当我使用 setAttribute 更改属性时,为什么我的自定义组件没有更新?

为什么自定义下拉菜单没有在点击时打开?

每次点击按钮时我都想更改图像

自定义WinForms按钮不会更改图像吗?

奇怪的问题,但为什么我没有点击按钮时我的表格正在提交

自定义单选按钮:为什么不显示图像?

为什么我的模态没有在点击添加区域按钮时关闭

当我点击提交按钮时,为什么值没有存储在表中?

为什么当我点击增量按钮时,步进器没有给出正确的值?

为什么我点击注册按钮时没有收到警报消息?

为什么当我点击按钮时相对节点的子节点没有删除?

为什么我的按钮在第一次被点击时没有反应?

为什么我的按钮没有点击?

在由笔尖制成的自定义表格单元格中单击时更改图像

如何在光滑的滑块中创建可在悬停时更改图像的自定义箭头?

为什么我的 RecyclerView 没有响应我的自定义 Click-Event?

自定义C#按钮单击任何想法时什么都没有?

谷歌地图搜索功能如何触发我在 JavaScript 中的自定义按钮点击(没有自动完成)

为什么我的自定义操作栏上的图像有多余的左填充

重新启动后为什么我的自定义鼠标光标没有加载?

为什么我的自定义分页器没有按预期加载结果?

为什么设置backgroundColor对我的自定义UIView没有可见效果?

为什么我的自定义登录页面没有显示在Spring Security 4中?

为什么没有绘制超出我的自定义视图边界的子视图?

为什么我的android自定义帐户没有显示在“帐户和设置”中?

为什么我的自定义菜单没有显示在Wordpress中?

为什么我的自定义测试没有被PHPUnit启动?

为什么我的自定义帖子类型没有显示在管理菜单中?