我知道这是一个常见的问题;但答案并不能满足我的要求。我试图在我的 tableview 单元格中有一个 mapView,我可以这样做;但是,当我滚动地图时,地图会重新开始重新加载,这会阻止平滑滚动。有人能帮帮我吗?这是来自 cellforrowatindexpath 的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *CellIdentifier = @"a";
FBGTimelineCell *cell = nil;
NSString *text = [object objectForKey:@"text"];
if (cell == nil) {
cell = [[FBGTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier detailText:text];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
/*
cell.tableViewBackgroundColor = tableView.backgroundColor;
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.backgroundColor = [UIColor whiteColor];
cell.cornerRadius = 2.0;
[cell prepareForTableView:self.tableView indexPath:indexPath];
*/
UIView *view = [[UIView alloc]initWithFrame:cell.frame];
view.layer.cornerRadius = 2;
view.layer.borderColor = [UIColor lightGrayColor].CGColor;
view.layer.borderWidth = 0.5;
view.backgroundColor = [UIColor whiteColor];
cell.backgroundView = view;
cell.backgroundColor = [UIColor clearColor];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd MMM yyyy"];
cell.dateLabel.text = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[object updatedAt]]];
cell.detailLabel.text = text;
cell.titleLabel.text = [object objectForKey:@"title"];
if ([[object objectForKey:@"isEvent"] integerValue] == 1) {
cell.photoView.hidden = YES;
PFQuery *query = [PFQuery queryWithClassName:@"Events"];
[query whereKey:@"objectId" equalTo:[object objectForKey:@"event"]];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
PFObject *object_event = [objects objectAtIndex:0];
NSDate *date = [object_event objectForKey:@"date"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MMM"];
cell.eventMonthLabel.text = [[formatter stringFromDate:date] uppercaseString];
[formatter setDateFormat:@"dd"];
cell.eventDayLabel.text = [formatter stringFromDate:date];
cell.eventInfoLabel.text = [object_event objectForKey:@"event"];
NSString *str = [object_event objectForKey:@"details"];
cell.eventDeatilsLabel.text = str;
CGSize size2 = [[object objectForKey:@"text"] sizeWithFont:[UIFont fontWithName:@"Helvetica" size:13] constrainedToSize:CGSizeMake(275, 300) lineBreakMode:NSLineBreakByClipping];
cell.backView.frame = CGRectMake(15, size2.height +58, 290, 150);
self.mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 150-65, 290, 65)];
self.mapView.mapType = MKMapTypeStandard;
[self.mapView setUserInteractionEnabled:NO];
[self.mapView setZoomEnabled:NO];
[self.mapView setScrollEnabled:NO];
CALayer *capa = self.mapView.layer;
//I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.mapView.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(2.0, 2.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.mapView.bounds;
maskLayer.path = maskPath.CGPath;
[capa addSublayer:maskLayer];
capa.mask = maskLayer;
[cell.backView addSubview:self.mapView];
UIView *separator = [[UIView alloc]initWithFrame:CGRectMake(0, 150-65, 290, 1)];
separator.backgroundColor = [UIColor colorWithHex:0xCBCBCB];
[cell.backView addSubview:separator];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(15, size2.height +58, 290, 150);
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:@selector(openEvent:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
self.mapView.delegate = self;
MKCoordinateRegion viewRegion;
MyLocation *location;
PFGeoPoint *geo =[object_event objectForKey:@"location"];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(geo.latitude, geo.longitude);
location = [[MyLocation alloc]initWithName:nil address:nil coordinate:coordinate];
viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, 1.0*METERS_PER_MILE, 1.0*METERS_PER_MILE);
[self.mapView addAnnotation:location];
//(41.06783368386694, 29.034912586212158)
[self.mapView setRegion:viewRegion animated:NO];
}];
}else{
if ([[object objectForKey:@"isPhoto"] integerValue] == 1) {
PFFile *theImage = [object objectForKey:@"photo"];
cell.photoView.backgroundColor = [UIColor darkGrayColor];
cell.backView.hidden = YES;
if (object) {
cell.photoView.file = [object objectForKey:@"photo"];
// PFQTVC will take care of asynchronously downloading files, but will only load them when the tableview is not moving. If the data is there, let's load it right away.
if ([cell.photoView.file isDataAvailable]) {
[cell.photoView loadInBackground];
}
}
NSData *imageData = [theImage getData];
UIImage *image = [UIImage imageWithData:imageData];
cell.photoView.userInteractionEnabled = YES;
cell.photoView.frame = CGRectMake(cell.photoView.frame.origin.x, cell.photoView.frame.origin.y, cell.photoView.frame.size.width, [self frameForImage:image inImageViewAspectFit:cell.photoView].size.height);
}else{
if ([[object objectForKey:@"isVideo"] integerValue] == 1) {
cell.photoView.frame = CGRectMake(cell.photoView.frame.origin.x, cell.photoView.frame.origin.y, cell.photoView.frame.size.width, 175);
cell.photoView.backgroundColor = [UIColor darkGrayColor];
cell.photoView.contentMode = UIViewContentModeScaleAspectFit;
playButton = [UIButton buttonWithType:UIButtonTypeCustom];
cell.backView.hidden = YES;
[playButton setImage:[UIImage imageNamed:@"play_button.png"] forState:UIControlStateNormal];
playButton.frame = cell.photoView.frame;
[cell addSubview:playButton];
if ([object objectForKey:@"photo"]) {
NSLog(@"das");
cell.photoView.file = [object objectForKey:@"photo"];
[cell.photoView loadInBackground];
cell.photoView.contentMode = UIViewContentModeScaleToFill;
}
url = [NSURL URLWithString:[object objectForKey:@"video"]];
[HCYoutubeParser thumbnailForYoutubeURL:url thumbnailSize:YouTubeThumbnailDefaultMaxQuality completeBlock:^(UIImage *image, NSError *error) {
if (!error) {
if (![object objectForKey:@"photo"] ) {
NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);
PFFile *imageFile = [PFFile fileWithName:@"videoImage.jpg" data:imageData];
NSLog(@"afsa");
[object setObject:imageFile forKey:@"photo"];
[object saveInBackground];
cell.photoView.image = image;
}
[playButton addTarget:self action:@selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
}
}];
cell.photoView.userInteractionEnabled = YES;
}else
cell.photoView.hidden = YES;
}
}
return cell;
}