Iphone UtableViewCell中的MKMapView

Iphone UtableViewCell中的MKMapView,iphone,ios,map,mkmapview,tableview,Iphone,Ios,Map,Mkmapview,Tableview,我知道这是一个常见的问题;但答案并不能满足我的要求。我试图在我的tableview单元格中有一个mapView,我可以做到这一点;但是,当我滚动地图时,它会再次开始重新加载,这会阻止平滑滚动。谁能帮帮我吗? 以下是CellForRowatineXpath中的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath object:(PF

我知道这是一个常见的问题;但答案并不能满足我的要求。我试图在我的tableview单元格中有一个mapView,我可以做到这一点;但是,当我滚动地图时,它会再次开始重新加载,这会阻止平滑滚动。谁能帮帮我吗? 以下是CellForRowatineXpath中的代码:

 - (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;

}

尽量避免将视图/对象分配到下一行之外

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.
    }
}

尽量避免将视图/对象分配到下一行之外

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.
    }
}

UITableView
滚动速度取决于将数据转换为像素的速度。可爱的麦金塔你有很多绘图代码

你能做什么? 你可以做各种各样的事情来使你的单元格渲染得更快:使用
CoreGraphics
绘制单元格,积极地缓存它们,或者简化它们。根据经验,避免复杂的UI元素层次结构

创建
NSDateFormatter
最多需要半秒钟的时间。创建一次,然后保留对它的引用,或者使用共享的“工厂”存储和创建
NSDateFormatter
实例

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

如果我没有告诉您,您使用的块可能包含对旧单元格的引用,以维护特定的上下文/范围,那我就错了。这个额外的(可能是
strong
)引用将单元格保留在内存中几秒钟

哦,天哪,这些都不行! 尝试使用
CoreGraphics
屏幕外
MKMapView
获取
UIImage
。确保每次都没有实例化一个新的实例,因为这可能会很昂贵

// 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
)来渲染地图,请参见我刚才写的要点:


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

UITableView
滚动速度取决于您将数据转换为像素的速度。可爱的麦金塔你有很多绘图代码

你能做什么? 你可以做各种各样的事情来使你的单元格渲染得更快:使用
CoreGraphics
绘制单元格,积极地缓存它们,或者简化它们。根据经验,避免复杂的UI元素层次结构

创建
NSDateFormatter
最多需要半秒钟的时间。创建一次,然后保留对它的引用,或者使用共享的“工厂”存储和创建
NSDateFormatter
实例

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

如果我没有告诉您,您使用的块可能包含对旧单元格的引用,以维护特定的上下文/范围,那我就错了。这个额外的(可能是
strong
)引用将单元格保留在内存中几秒钟

哦,天哪,这些都不行! 尝试使用
CoreGraphics
屏幕外
MKMapView
获取
UIImage
。确保每次都没有实例化一个新的实例,因为这可能会很昂贵

// 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
)来渲染地图,请参见我刚才写的要点:


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

设置单元格的代码在哪里?静态NSString*CellIdentifier=@“a”;FBGTimelineCell*单元=nil;NSString*text=[objectobjectforkey:@“text”];如果(cell==nil){cell=[[FBGTimelineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier detailText:text];}在何处向单元格添加映射视图?在CellForRowatinexPath Too中,可以显示整个方法的代码吗?设置单元格的代码在何处?静态NSString*CellIdentifier=@“a”; FBGTimelineCell*单元=nil;NSString*text=[objectobjectforkey:@“text”];if(cell==nil){cell=[[FBGTimelineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier detailText:text];}在何处将映射视图添加到单元格?在cellforrowatindexpath中,可以显示整个方法的代码吗?但映射仍将重新加载。但映射仍将重新加载。这是一个很好的代码示例