UITableView 的didSelectRowAtIndexPath和didDeselectRowAtIndexPath

原文地址::https://www.cnblogs.com/qianyindichang/p/4284061.html

 

相关文章

1、TableView didSelectRowAtIndexPath 不执行----https://www.cnblogs.com/jasonduan/p/4950788.html

2、UITableView的代理方法didSelectRowAtIndexPath与手势点击的冲突----https://www.jianshu.com/p/1f9ecc9a290a

 

今天被UITableView给坑了一道,我写了一个横向的UITableView

如图,通过这两个方法实现需求的触发事件,但是能同时选中两个index,

据说UITableView不会管视图外的indexpath,所以重点来了

自己定义一个indexpath,直接上代码

 NSIndexPath *_selectedIdxPath;

其他的都跟平常的UITableView一样的,只是初始化cell的时候需要这么做

复制代码

//这是自定义的cell,按自己的需求来
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"filiter";
   FilterChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[FilterChooseTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setImgViewImg:[UIImage imageNamed:_filiterData[indexPath.row]]];
    cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);
//这是未选中的调用的方法,初始化cell的时候,恢复原状
[cell DownVideoWitnAnimation:NO]; 
if (indexPath == _selectedIdxPath)
 { 
[cell MoveUpVideoWithAnimation:NO];
 }
 return cell; 
}

复制代码

复制代码

//选中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FilterChooseTableViewCell *cell = (FilterChooseTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    [cell MoveUpVideoWithAnimation:YES];

       _selectedIdxPath = indexPath;


 
}

复制代码

复制代码

//未选中
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FilterChooseTableViewCell *cell = (FilterChooseTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    [cell DownVideoWitnAnimation:YES];
}

复制代码

展开阅读全文

DidSelectRowAtIndexPath调用失败

02-18

在masterTableView用到三个不同的detailTableViews ,触屏时会调用,但是没有调用成。rnrnmaster.m文件:rnrn #import "GuideTableViewController.h"rn #import "GuideDetailTableViewController.h"rn #import "GuideDetailTableViewController2.h"rn #import "GuideDetailTableViewController3.h"rn #import rn rn @interface GuideTableViewController ()rn rn NSMutableData *weatherResponseData;rn rn NSArray *headGuide;rn rn NSArray *leftImages;rn rn rn rn @property (weak, nonatomic) IBOutlet UITableView *tableView;rn rn rn @property (weak, nonatomic) IBOutlet UIImageView *imgHeader;rn rn rn @property (weak, nonatomic) IBOutlet UIImageView *ImgTitle;rn rn rn @property (weak, nonatomic) IBOutlet UIImageView *ImgWeather;rn rn rn @property (weak, nonatomic) IBOutlet UIButton *btnMap;rn rn @property (weak, nonatomic) IBOutlet UILabel *LabelWeather;rn rn @property (weak, nonatomic) IBOutlet UILabel *LabelWeather2;rn rn @endrn rn @implementation GuideTableViewControllerrn rn - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNilrn rn self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];rn if (self) rn // Custom initializationrn rn return self;rn rn //Weather methodrn rn - (void) loadWeatherrn rn NSURLRequest *theRequest = [NSURLRequest requestWithURL:rn [NSURL URLWithString:@"http://api.wunderground.com/api/3919480da5014c98/conditions/q/BR/Sao_Sebastiao .json"]];rn NSURLConnection *theConnection=[[NSURLConnection alloc]rn initWithRequest:theRequest delegate:self];rn if(theConnection)rn weatherResponseData = [[NSMutableData alloc] init];rn else rn NSLog(@"failed");rn rn rn rn //Delegates for WeatherDatarn rn - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)responsern rn [weatherResponseData setLength:0];rn rn rn -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)datarn rn [weatherResponseData appendData:data];rn rn rn - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)errorrn rn NSString *msg = [NSString stringWithFormat:@"Failed: %@", [error description]];rn NSLog(@"%@",msg);rn rn rn - (void)connectionDidFinishLoading:(NSURLConnection *)connectionrn rn NSError *myError = nil;rn NSDictionary *res = [NSJSONSerialization JSONObjectWithData:weatherResponseData options:NSJSONReadingMutableLeaves error:&myError];rn NSArray *results = [res objectForKey:@"current_observation"];rn NSString *cur = [results valueForKey:@"weather"];rn NSString *tmp = [results valueForKey:@"temperature_string"];rn NSString *wind = [results valueForKey:@"wind_string"];rn rn rn NSLog(@"Current conditions: %@, %@º, %@", cur, tmp, wind);rn rn rn self.LabelWeather.text = cur;rn rn self.LabelWeather2.text = tmp;rn rn rn rn //JSONmetodrn rn - (void) loadJSONrn dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^rn //codern NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://dl.dropbox.com/u/100670549/guide.json"]];rn rn NSError *error;rn rn if (data)rn rn rn headGuide = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];rn rn for (NSDictionary *dictionary in headGuide)rn // NSLog([dictionary description]);rn rn rn elsern rn NSLog(@"Could not load data");rn rn dispatch_sync(dispatch_get_main_queue(), ^rn // codern rn [self.tableView reloadData];rn );rn );rn rn rn //Loadrn rn - (void)viewDidLoadrn rn [super viewDidLoad];rn // Do any additional setup after loading the view.rn rn [self loadJSON];rn [self loadWeather];rn rn leftImages = [NSArray arrayWithObjects:@"btn_Stay.png", @"btn_Eat.png", @"btn_Todo.png", nil];rn //set backgroundrn self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.jpg"]];rn rn //rounded cornersrn rn [self.tableView.layer setCornerRadius:9.0];rn rn [self.ImgWeather.layer setCornerRadius:9.0];rn rn rn rn - (void)didReceiveMemoryWarningrn rn [super didReceiveMemoryWarning];rn // Dispose of any resources that can be recreated.rn rn rn #pragma mark - Table view data sourcern rn - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableViewrn rn // Return the number of sections.rn return 1;rn rn rn - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionrn rn // Return the number of rows in the section.rn return headGuide.count;rn rn rn -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathrn rn UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];rn rn rn NSArray *dict = [headGuide objectAtIndex:indexPath.row];rn rn cell.textLabel.text = [dict valueForKey:@"title"];rn rn NSString *cellImage = [leftImages objectAtIndex:indexPath.row];rn UIImage *cellIcon = [UIImage imageNamed:cellImage];rn rn cell.imageView.image = cellIcon;rn rn return cell;rn rn rn - (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)senderrn if ([segue.identifier isEqualToString:@"whereStay"])rn GuideDetailTableViewController *vc = [segue destinationViewController];rn NSIndexPath *index = sender;rn NSDictionary *dict = [headGuide objectAtIndex:index.row];rn vc.stayGuide = dict;rn rn else if ([segue.identifier isEqualToString:@"whereEat"])rn GuideDetailTableViewController2 *vc1 = [segue destinationViewController];rn NSIndexPath *index = sender;rn NSDictionary *dict = [headGuide objectAtIndex:index.row];rn vc1.eatGuide = dict;rn rn else if ([segue.identifier isEqualToString:@"whatTodo"])rn GuideDetailTableViewController3 *vc2 = [segue destinationViewController];rn NSIndexPath *index = sender;rn NSDictionary *dict = [headGuide objectAtIndex:index.row];rn vc2.todoGuide = dict;rn rn rn rn #pragma mark - tableView delegatern rn - (void)tableView:(UITableView *)tableView didselectRowAtIndexPath:(NSIndexPath *)indexPathrn if(indexPath.row == 0)rn [self performSegueWithIdentifier:@"whereStay" sender:indexPath];rn else if(indexPath.row ==1 )rn [self performSegueWithIdentifier:@"whereEat" sender:indexPath];rn elsern [self performSegueWithIdentifier:@"whatTodo" sender:indexPath];rn rn rn [tableView setAllowsSelection:YES];rn rn rn @endrnrn 问答

没有更多推荐了,返回首页