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

2014年8月17日 星期日

iOS 開發教學 - UITableViewController 與 UIRefreshControl 用法 (Pull to Refresh)

呃,太久沒摸 UITableViewController 了,有一陣子都用 Storyboard 去刻,最近想說返樸歸真,改用純 coding 的方式,馬上想到如何處理 Pull to refresh 這個議題。

因為用 CocoaPods 一陣子,馬上就用關鍵字搜尋一下,發現有幾款不錯,但總是覺得差那麼一點,例如超過一年沒更新,當年熱門的甚至四年沒更新了 Orz 想了一會兒,才驚醒是不是變成內建了 XD 才想到之前去年底複習 CS193P 時,就看到教學過,那時是使用 Storyboard 的用法,隨意勾一勾就有的東西。

總之,回到主題,純寫 code 的方式,以 UITableViewController 而已,就是配置 self.refreshControl 就對了!

- (void)refreshData
{
    self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Loading..."];
    dispatch_async(dispatch_queue_create("loading...", NULL), ^{
        [NSThread sleepForTimeInterval:5];
      
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.refreshControl endRefreshing];
            self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"];
        });
    });
}

- (void)viewWillAppear:(BOOL)animated
{
    if (!self.refreshControl)
        self.refreshControl = [[UIRefreshControl alloc] init];
    if (!self.refreshControl.isRefreshing) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView setContentOffset:CGPointMake(0, -self.refreshControl.frame.size.height - self.navigationController.navigationBar.frame.size.height) animated:YES];
            [self.refreshControl beginRefreshing];
            [self refreshData];
        });
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (!self.refreshControl)
        self.refreshControl = [[UIRefreshControl alloc] init];
    if (self.refreshControl) {
        self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull to refresh"];
        [self.refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged];
    }
}


雖然很簡單,還是筆記一下 Orz

2013年12月15日 星期日

iOS 開發筆記 - UITableViewController Editing Mode 隱藏 Delete 按鈕(Move Only)

UITableViewControllerEditMode

開始把玩 UITableViewController Editing mode,由於不想讓使用者刪資料,所以就需要把 Delete 相關的操作介面給關掉。

簡言之:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //if (self.editing)
    //    return UITableViewCellEditingStyleDelete;
 
    return UITableViewCellEditingStyleNone;
}


如此一來即可:

UITableViewControllerEditModeWithoutDeletition

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
}