顯示具有 uitableviewcell 標籤的文章。 顯示所有文章
顯示具有 uitableviewcell 標籤的文章。 顯示所有文章

2015年10月3日 星期六

iOS 開發筆記 - UITableViewCell 在 iOS9 的環境中,預設沒有佔滿 UITableView Width

假設 UITableView 的寬度有 768,但 UITableViewCell 拿不到 768 ,導致用程式計算的排版會出錯,且連 titleForHeaderInSection 顯示也一樣,追了一下應該是 Margin 效果

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UITableView_Class/#//apple_ref/occ/instp/UITableView/cellLayoutMarginsFollowReadableWidth

偷懶解法:

if([self.tableView respondsToSelector:@selector(setCellLayoutMarginsFollowReadableWidth:)])
self.tableView.cellLayoutMarginsFollowReadableWidth = NO;

2013年12月9日 星期一

iOS 開發筆記 - Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier myCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

AttriubuteTableViewCellIndentifier DocumentOutlineTableViewCell

最近摸 Storyboard 一陣子,偶爾還會犯一些傻 bug ,就是 Identifier 設錯地方 XD 例如跑去 Identity Inspector 的 Identity 之 Restoration ID :

IndentityTableViewCellRestorationID

結果程式一跑時,就噴訊息了:

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier myCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

解法就是開啟 storyboard 後,仔細查看左邊的 Document Outline 之 UITableViewController 裡的 Table View Cell 是不是有被標記起來,接著去看看右邊 Attributes Inspector 顯示的 Identifier 是不是跟程式碼一樣的,如此就能 debug 啦。

忘記是不是 iOS 5 SDK 開始,預設的 UITableViewController 的範例程式都是寫成綁定在 Interface builder (xib) / Storyboard 的寫法:

static NSString *CellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


早期 iOS 4 SDK 預設 UITableViewController 之 (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 寫法:

static NSString *CellIdentifier = @"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}


早期 Interface builder (xib) 的用法,須寫程式註冊 Nib:

- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@“MyCell" bundle:nil]
forCellReuseIdentifier:@“myCell”]; // "MyCell" for MyCell.xib
}