UITableViewCell中的MKMapView

时间:2013-06-14 19:09:54

标签: iphone ios map mkmapview tableview

我知道这是一个常见问题;但答案并不能满足我的要求。我试图在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;

}

2 个答案:

答案 0 :(得分:1)

UITableView滚动速度取决于您将数据转换为像素的速度。甜蜜macintosh你有很多绘图代码!!

你能做什么?

您可以执行各种操作以使您的单元格更快地渲染:使用CoreGraphics绘制单元格,积极缓存它们或简化它们。根据经验,避免使用复杂的UI元素层次结构。

创建NSDateFormatter可能需要半秒。创建一次,然后保存对它的引用,或使用共享的“工厂”来存储和创建NSDateFormatter实例。

MKMapView滚动也有可能与您的UITableView滚动竞争。禁用MKMapView上的滚动。

如果我没有告诉你,你使用的块可能包含对旧单元格的引用以维持某个上下文/范围,那么我会感到不舒服。这个额外的(可能strong)引用将单元格保留在内存中几秒钟。

哦,拍摄,这都不起作用!

尝试使用CoreGraphics屏幕外 UIImage获取MKMapView。确保你每次都没有实例化新的,因为这可能很昂贵。

// Untested

UIGraphicsBeginImageContextWithOptions(_mapView.frame.size, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();

CGAffineTransform flipVertical = CGAffineTransformMake( 1, 0, 0, -1, 0, _mapView.frame.size.height);
CGContextConcatCTM(context, flipVertical);  

[_mapView.layer renderInContext:context];

UIImage *renderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// Do stuff with renderedImage

此图片可以无限期缓存,地图不会经常更改。

如果您真的想要获得幻想,可以创建一个包含此MKMapView的共享实例并渲染地图的一部分。您可以在MKMapView中挂钩到地图图块渲染,而不是使用上面的代码。

更新: (6.24.14)

在iOS 7中,您可以使用专门的类(您不需要再持有MKMapView来渲染地图),请参阅我刚才写的这个Gist:

https://gist.github.com/fhsjaagshs/2e0471b41bb7666efa7d

这种方法更好,因为您不需要保留MKMapView,您不需要编写任何可能很慢的绘图代码(CoreGraphics是基于CPU的绘图,{{1可能是基于GPU的),它是异步的!您可以将MKMapSnapshotter放入您的单元格中,当回调加载时,将其删除并将其替换为UIActivityIndicatorView

答案 1 :(得分:0)

尽量避免在以下行中分配视图/对象。

if (cell == nil) {
    cell =  [[FBGTimelineCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier detailText:text];

    // Set Cell background View

    // Create Map View Here

    if(!self.mapview)
    {
        // Alloc Here.
    }
}