我正在尝试使附件视图类似于Apple的演示,但我一定错过了一些东西,因为单击时我的图像不会切换。它只是闪烁。
不知道我错过了什么,我试图将其完全基于演示代码。我不得不将几个字典更改为可变的,并添加了if单元格空子句以使我的工作正常。
项目已获取并正确显示在表格视图中。
我鼓舞了我所改变的部分,我想我都明白了。
任何帮助,非常感谢。
我的代码:
标头声明:
@interface PollQueryController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@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] 删除。
我来说两句