簡體   English   中英

帶有UITableViewcell的情節提要-自定義表格視圖單元格的外觀

[英]storyboard with UITableViewcell - customize the appearence of the table view cell

我試圖使用添加約束來自定義表中單元格的外觀。

代碼的當前圖像,不添加約束

我正在嘗試添加約束,以便為所有方案提供自動放置。 以下是texfield的def。

UITextField *txt = [[UITextField alloc] initWithFrame:CGRectMake(0, 7, 170, 22)];
txt.placeholder = @"Welcome";
txt.font = kFontSize16;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
txt.returnKeyType = UIReturnKeyNext;
txt.translatesAutoresizingMaskIntoConstraints = NO;
txt.delegate = self;
txt.textColor = kColorDeviceListStatusLabel;
txt.keyboardType = UIKeyboardTypeDecimalPad;
self.txthello = txt;

下面是UITableViewCell的定義:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.font = myfont;

cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.text = @"From:";
cell.contentView addSubview:self.txthello];
NSLog(@"working till here");

[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtIPStart
                                                                     attribute:NSLayoutAttributeLeading
                                                                     relatedBy:NSLayoutRelationEqual
                                                                     toItem: cell.contentView
                                                                     attribute:NSLayoutAttributeLeft
                                                                     multiplier:1.0
                                                                    constant:90.0]];

NSLog(@" the log is not displayed here.");
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtIPStart
                                                                     attribute:NSLayoutAttributeTrailing
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:cell.contentView
                                                                     attribute:NSLayoutAttributeRight
                                                                    multiplier:1.0
                                                                      constant:-50.0]];
return cell;

由於某種原因,當我模擬時,它在toItem:cell.contentView部分失敗

錯誤是線程1:信號異常終止,不確定是什么原因造成的。

提前致謝。

更新:下面是堆棧崩潰。

0   tableSample                  0x0000000101f61bd7 -[SettingViewController layoutView] + 55
1   tableSample                  0x0000000101f6864c -[SettingViewController viewDidLoad] + 76
2   tableSample                  0x000000010200c80e __47+[UIViewController(Hooks) swizzle_viewDidLoad:]_block_invoke + 69
3   UIKit                               0x00000001030e3931 -[UIViewController loadViewIfRequired] + 1344
4   UIKit                               0x0000000103126c26 -[UINavigationController _layoutViewController:] + 54
5   UIKit                               0x00000001031274dd -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 433
6   UIKit                               0x0000000103127633 -[UINavigationController _startTransition:fromViewController:toViewController:] + 116
7   UIKit                               0x0000000103128879 -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
8   UIKit                               0x000000010312967d -[UINavigationController __viewWillLayoutSubviews] + 57
9   UIKit                               0x00000001032c163d -[UILayoutContainerView layoutSubviews] + 248
10  UIKit                               0x000000010300911c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
11  QuartzCore                          0x000000010276036a -[CALayer layoutSublayers] + 146
12  QuartzCore                          0x0000000102754bd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
13  QuartzCore                          0x0000000102754a4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14  QuartzCore                          0x00000001027491d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
15  QuartzCore                          0x00000001027769f0 _ZN2CA11Transaction6commitEv + 508
16  UIKit                               0x0000000102f8253a _afterCACommitHandler + 174
17  CoreFoundation                      0x0000000104fa79d7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18  CoreFoundation                      0x0000000104fa7947 __CFRunLoopDoObservers + 391
19  CoreFoundation                      0x0000000104f9d59b __CFRunLoopRun + 1147
20  CoreFoundation                      0x0000000104f9ce98 CFRunLoopRunSpecific + 488
21  GraphicsServices                    0x0000000106196ad2 GSEventRunModal + 161
22  UIKit                               0x0000000102f58676 UIApplicationMain + 171
23  tableSample                  0x0000000101fba91f main + 111
24  libdyld.dylib                       0x000000010584192d start + 1

更新2:您好...捕獲到異常*** + [NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]:無法在前/后屬性和右/左屬性之間建立約束。 兩者都不使用前導/尾隨。

從代碼設置約束時,應將setTranslatesAutoresizingMaskIntoConstraints設置為NO

[centerView setTranslatesAutoresizingMaskIntoConstraints:NO];

希望這會幫助你。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM