comparison ios/dw.m @ 2829:c543f7df7867

iOS: Implement layout constraints to put the UIStackView containing the columns below the row's image and text label and attempt MULTI mode. Need to figure out how to add the stack's extra height to the cell. Multi mode is implemented with UIButtons and I am getting a weird selector sent to the buttons when in Multi mode.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Tue, 06 Sep 2022 07:07:19 +0000
parents 455d539ac555
children cc9258ba53d7
comparison
equal deleted inserted replaced
2828:2c64d04abcf1 2829:c543f7df7867
2356 if(columndata && _dw_container_mode > DW_CONTAINER_MODE_DEFAULT) 2356 if(columndata && _dw_container_mode > DW_CONTAINER_MODE_DEFAULT)
2357 { 2357 {
2358 /* If we don't have a stack, create one */ 2358 /* If we don't have a stack, create one */
2359 if(!stack) 2359 if(!stack)
2360 { 2360 {
2361 NSLayoutConstraint *constraint;
2362
2361 stack = [[[UIStackView alloc] init] retain]; 2363 stack = [[[UIStackView alloc] init] retain];
2362 [stack setTranslatesAutoresizingMaskIntoConstraints:NO]; 2364 [stack setTranslatesAutoresizingMaskIntoConstraints:NO];
2363 [stack setSpacing:5.0]; 2365 [stack setSpacing:5.0];
2364 [[self contentView] addSubview:stack]; 2366 [[self contentView] addSubview:stack];
2365 } 2367
2366 /* Extra mode creates a horizontal stack with all the column data */ 2368 /* Leading */
2367 if(_dw_container_mode == DW_CONTAINER_MODE_EXTRA) 2369 constraint = [NSLayoutConstraint constraintWithItem:stack attribute:NSLayoutAttributeLeft
2368 { 2370 relatedBy:NSLayoutRelationEqual toItem:[self contentView]
2371 attribute:NSLayoutAttributeLeft multiplier: 1.0 constant:0.0];
2372 [[self contentView] addConstraint:constraint];
2373
2374 /* Top */
2375 constraint = [NSLayoutConstraint constraintWithItem:stack attribute:NSLayoutAttributeTop
2376 relatedBy:NSLayoutRelationEqual toItem:[self textLabel]
2377 attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
2378 [[self contentView] addConstraint:constraint];
2379 }
2380 /* Extra and Multi modes create a stack with all the column data */
2381 if(_dw_container_mode > DW_CONTAINER_MODE_DEFAULT)
2382 {
2383 bool extra = _dw_container_mode == DW_CONTAINER_MODE_EXTRA;
2369 id subviews = [stack arrangedSubviews]; 2384 id subviews = [stack arrangedSubviews];
2370 int index = 0; 2385 int index = 0;
2371 2386
2372 [stack setAxis:UILayoutConstraintAxisHorizontal]; 2387 /* Extra mode stack is horizontal, multi stack is vertical */
2388 [stack setAxis:(extra ? UILayoutConstraintAxisHorizontal : UILayoutConstraintAxisVertical)];
2389
2373 /* Create the stack using columndata, reusing objects when possible */ 2390 /* Create the stack using columndata, reusing objects when possible */
2374 for(id object in columndata) 2391 for(id object in columndata)
2375 { 2392 {
2376 if([object isKindOfClass:[NSString class]]) 2393 if([object isKindOfClass:[NSString class]])
2377 { 2394 {
2378 UILabel *label = nil; 2395 id label = nil;
2379 2396
2397 /* Check if we already have a view, reuse it if possible.. */
2380 if(index < [subviews count]) 2398 if(index < [subviews count])
2381 { 2399 {
2382 id oldview = [subviews objectAtIndex:index]; 2400 id oldview = [subviews objectAtIndex:index];
2383 2401
2384 if([oldview isMemberOfClass:[UILabel class]]) 2402 if([oldview isMemberOfClass:(extra ? [UILabel class] : [UIButton class])])
2403 {
2385 label = oldview; 2404 label = oldview;
2405 /* If we are reusing a button, make sure the image is not set */
2406 if(!extra)
2407 [label setImage:nil forState:UIControlStateNormal];
2408 }
2386 else 2409 else
2387 [stack removeArrangedSubview:oldview]; 2410 [stack removeArrangedSubview:oldview];
2388 } 2411 }
2389 if(!label) 2412 if(!label)
2390 { 2413 {
2391 label = [[UILabel alloc] init]; 2414 label = extra ? [[UILabel alloc] init] : [UIButton buttonWithType:UIButtonTypeCustom];
2392 2415
2393 [label setTranslatesAutoresizingMaskIntoConstraints:NO]; 2416 [label setTranslatesAutoresizingMaskIntoConstraints:NO];
2394 [label setTextAlignment:NSTextAlignmentCenter]; 2417 if(extra)
2418 [label setTextAlignment:NSTextAlignmentCenter];
2395 2419
2396 if(index < [subviews count]) 2420 if(index < [subviews count])
2397 [stack insertArrangedSubview:label atIndex:index]; 2421 [stack insertArrangedSubview:label atIndex:index];
2398 else 2422 else /* Remove the view if it won't work */
2399 [stack addArrangedSubview:label]; 2423 [stack addArrangedSubview:label];
2400 } 2424 }
2401 [label setText:object]; 2425 /* Set the label or button text/title */
2426 if(extra)
2427 [label setText:object];
2428 else
2429 [label setTitle:object forState:UIControlStateNormal];
2402 index++; 2430 index++;
2403 } 2431 }
2404 else if([object isMemberOfClass:[UIImage class]]) 2432 else if([object isMemberOfClass:[UIImage class]])
2405 { 2433 {
2406 UIImageView *image = nil; 2434 id image = nil;
2407 2435
2436 /* Check if we already have a view, reuse it if possible.. */
2408 if(index < [subviews count]) 2437 if(index < [subviews count])
2409 { 2438 {
2410 id oldview = [subviews objectAtIndex:index]; 2439 id oldview = [subviews objectAtIndex:index];
2411 2440
2412 if([oldview isMemberOfClass:[UIImageView class]]) 2441 if([oldview isMemberOfClass:(extra ? [UIImageView class] : [UIButton class])])
2442 {
2413 image = oldview; 2443 image = oldview;
2414 else 2444 /* If we are reusing a button, make sure the text is not set */
2445 if(!extra)
2446 [image setTitle:nil forState:UIControlStateNormal];
2447 }
2448 else /* Remove the view if it won't work */
2415 [stack removeArrangedSubview:oldview]; 2449 [stack removeArrangedSubview:oldview];
2416 } 2450 }
2417 if(!image) 2451 if(!image)
2418 { 2452 {
2419 image = [[UIImageView alloc] initWithImage:object]; 2453 image = extra ? [[UIImageView alloc] init] : [UIButton buttonWithType:UIButtonTypeCustom];
2420 2454
2421 [image setTranslatesAutoresizingMaskIntoConstraints:NO]; 2455 [image setTranslatesAutoresizingMaskIntoConstraints:NO];
2422 2456
2423 if(index < [subviews count]) 2457 if(index < [subviews count])
2424 [stack insertArrangedSubview:image atIndex:index]; 2458 [stack insertArrangedSubview:image atIndex:index];
2425 else 2459 else
2426 [stack addArrangedSubview:image]; 2460 [stack addArrangedSubview:image];
2427 } 2461 }
2462 /* Set the image view or button image */
2463 if(extra)
2464 [image setImage:object];
2428 else 2465 else
2429 [image setImage:object]; 2466 [image setImage:image forState:UIControlStateNormal];
2467
2430 index++; 2468 index++;
2431 } 2469 }
2432 } 2470 }
2433 } 2471 }
2434 } 2472 }