2014년 5월 28일 수요일

append items to UITableView or UICollectionView


// from stack overflow with UITableView

// build the index paths for insertion
    // since you're adding to the end of datasource, the new rows will start at count
    NSMutableArray*indexPaths =[NSMutableArray array];
    NSInteger currentCount = self.datasource.count;
    for(int i =0; i < dataToAdd.count; i++)
    {
        [indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]];
    }
    // do the insertion
    [self.dataSource addObjects:dataToAdd];
    // tell the table view to update (at all of the inserted index paths)
    [self.tableView beginUpdates];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];

    [self.tableView endUpdates];



// in my real code with UICollectionView

- (void)getMoreServerData
{
// 새로운 아이템을 컬렉션의 끝에 추가
NSMutableArray *indexPaths = [NSMutableArray array];
                NSInteger currentCount = items.count;
                for (int i = 0; i < model.storesModelArray.count; i++) {
                    [indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]];
                }
                [items addObjectsFromArray:model.storesModelArray];
                [self.collectionView insertItemsAtIndexPaths:indexPaths];
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// 리스트의 마지막에 도착한 경우 attach data
    if (scrollOffset + scrollView.frame.size.height == scrollView.contentSize.height)
    {
        [self getMoreServerData];
    }
}

2014년 5월 17일 토요일

grid crop with uiimage

이미지를 grid 형태로 잘라서 view에 add하는 예제.


- (void)viewDidLoad
{
    [super viewDidLoad];
    UIImage *scaledImage = [self scaleWithImage:[UIImage imageNamed:@"37.jpg"] scaledToSize:self.view.frame.size];
    self.originalImageView = [[UIImageView alloc] initWithImage:scaledImage];
    [self prepareSlices:10 :10];
}

-(void)prepareSlices:(uint)row :(uint)col
{
    float flagX = _originalImageView.image.size.width / _originalImageView.frame.size.width;
    float flagY = _originalImageView.image.size.height / _originalImageView.frame.size.height;
    
    float _width    = _originalImageView.frame.size.width / col;
    float _height   = _originalImageView.frame.size.height / row;
    
    float _posX = 0.0;
    float _posY = 0.0;
    
    for (int i = 1; i <= row * col; i++) {
        
        UIImageView *croppedImageVeiw = [[UIImageView alloc] initWithFrame:CGRectMake(_posX, _posY, _width, _height)];
        UIImage *img = [self getCropImage:CGRectMake(_posX * flagX,_posY * flagY, _width * flagX, _height * flagY)];
        croppedImageVeiw.image = img;
        
        croppedImageVeiw.layer.borderColor = [[UIColor whiteColor] CGColor];
        croppedImageVeiw.layer.borderWidth = 1.0f;
        
        [self.view addSubview:croppedImageVeiw];
        
        _posX += _width;
        
        if (i % col == 0) {
            _posX = 0;
            _posY += _height;
        }
    }
}

-(UIImage*)getCropImage:(CGRect)cropRect
{
    CGImageRef image = CGImageCreateWithImageInRect([_originalImageView.image CGImage],cropRect);
    UIImage *cropedImage = [UIImage imageWithCGImage:image];
    CGImageRelease(image);
    return cropedImage;

}

- (UIImage *)scaleWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    //UIGraphicsBeginImageContext(newSize);
    // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution).
    // Pass 1.0 to force exact pixel size.
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 1.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}


2014년 5월 10일 토요일

UIView가 화면에 추가되서 보여지는지 안보여지고있는지 체크하는 방법

UIView.window를 체크하면 됨.

<예제>
MyUIViewController.h

UIView *popupView;



MyUiViewController.m
..
popupView = [UIView alloc] init];
[self.view addSubView:popupView];

if(popupView.Window  != nil)
    NSLog(@"you can see popup now");



....

[popupView removeFromSuperView];
if(popupView.Window  == nil)
    NSLog(@"you can't see popup now");


2014년 5월 9일 금요일

uiview 터시하면 keyboard 사라지게 하는 방법

UIView 의 - (BOOL)endEditing:(BOOL)force  함수를 사용하면 됨.


예제)

MyViewController.m
...
..
[self.view endEditing:NO];

2014년 5월 2일 금요일

string에서 keyword를 찾아 빨간색으로 표시하기




// search matching keyword
    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:searchKeyword
                                  options:NSRegularExpressionCaseInsensitive
                                  error:nil];
    [regex enumerateMatchesInString:fullString options:0 range:NSMakeRange(0, [fullString length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop){
        [attributedText addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:result.range];
        
        [self.label setAttributedText:attributedText];

    }];