Wednesday, July 24, 2013

Block example

в классе

- (NSArray *)loadTitles
{
    NSMutableSet *set = [NSMutableSet set];
    
    NSArray *lists = [List findAll];
    for (List *list in lists) {
        NSSet *titles = [list.items select:^id(id element) {
            ListItem *item = element;
            return item.title;
        }];
        
        [set addObjectsFromArray:titles.allObjects];
    }
    
    return [set.allObjects sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}

- (void)loadTitlesWithAction:(ArrayBlock)action
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *titles = [self loadTitles];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (action)
                action(titles);
        });
    });

}

вызов в другом классе
     // пример использования метода с блоком
    // вызываем метод и передаем блок, которые вызовется, когда метод закончится
    // в итоге получаем то же, что и с делегатом, но без мороки с делегатом
    self.allItemsInListForAutoComplete = [[AllItemsInList alloc] init];
    [self.allItemsInListForAutoComplete loadTitlesWithAction:^(NSArray *arr) {
        self.titleItemForAutoCompleteArray = arr.mutableCopy;
        [self.autocompleteTableView reloadData];
    }];


Tuesday, July 16, 2013

section UITableView not stopped

1) UITableViewStyleGrouped
2) tableView.backgroundView = nil;
3) в cellForRow...        
    cell.backgroundView = [[[UIView alloc] initWithFrame:cell.bounds] autorelease];
4) в layoutSubviews ячейки
    self.contentView.frame = self.bounds;

resize iPad

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    smokeKillYou = [[UILabel alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 108 , self.view.frame.size.width, 108)];
    smokeKillYou.numberOfLines = 1;
    smokeKillYou.font = [UIFont fontWithName:@"Arial" size:100];
    smokeKillYou.textColor = [UIColor blackColor];
#warning шрифт не тот
    smokeKillYou.text = @"TEST";
    smokeKillYou.textAlignment = NSTextAlignmentCenter;
//    smokeKillYou.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
    [self.view addSubview:smokeKillYou];
    [smokeKillYou release];
    }



-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    [self resizeWithInterfaceOrientation:toInterfaceOrientation];
}

- (void) resizeWithInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation
{
    CGFloat width = UIInterfaceOrientationIsLandscape(interfaceOrientation)? 1024 : 768;
    CGFloat height = UIInterfaceOrientationIsLandscape(interfaceOrientation)? 768 : 1024;
    
    smokeKillYou.frame = CGRectMake(0, height - 108 , width, 108);

}