2011-12-09 27 views
0

私はInterface Builderでプロトタイプのセルを作成しました。このプロトタイプには、文字列の長さに応じてサイズを変更するイメージビューが含まれています。 したがって、私はUITableViewCellのサブクラスで、指定されたフォントでテキスト文字列の幅を計算し、この幅をイメージビューの幅に設定するメソッドを作成しました。 これは、別のビューコントローラにプッシュしてから、自分のテーブルビューに戻るまでうまくいきます。次に、ビューは約20ピクセル右に再配置されました。UITableViewCellサブクラスのラベルが位置を変更します

これはなぜ起こるのですか?これは、x位置を何か静的に設定した場合でも起こります。 200

アップデート1:私はのUITableViewCell、DFolderCellの私のサブクラスを使用して、私の- (UITableViewCell *)tableView:cellForRowAtIndexPath:

。これはcountstarredラベルの値を設定し、それらのラベルと画像を_count_starredの幅の幅に指定したフォントでcountIconstarredIconの値に設定するメソッド- (void)updateIndicatorsCount:andStarredを呼び出します。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"folderCell"; 
    DFolderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[DFolderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    int index = indexPath.row; 
    Folder *folder = (Folder *) [folders objectAtIndex:index]; 
    FolderIcon icon = (FolderIcon) folder.icon.intValue; 

    cell.name.text = folder.name; 
    cell.icon.image = [[DFolders sharedFolders] imageForFolderIcon:icon]; 

    int count = [[DDrafts sharedDrafts] amountOfDraftsInFolder:folder withType:DraftLoadTypeRecent]; 
    int starred = [[DDrafts sharedDrafts] amountOfDraftsInFolder:folder withType:DraftLoadTypeStarred]; 
    [cell updateIndicatorsCount:count andStarred:starred]; 

    return cell; 
} 

以下の方法は、これが結果である私のサブクラスの一部DFolderCell

/* Update size of count and starred indicators */ 
- (void)updateIndicatorsCount:(int)_count andStarred:(int)_starred 
{ 
    UIFont *badgeFont = [UIFont systemFontOfSize:11.0]; 

    UIImage *background; 

    _count = 1000; // Test 

    if (_count > 0) 
    { 
     self.count.text = [NSString stringWithFormat:@"%i", _count]; 

     CGSize size = [self.count.text sizeWithFont:badgeFont]; 

     CGRect frame = self.countIcon.frame; 
     frame.size.width = size.width; 
     frame.origin.x = 320 - 20 - size.width; 
     self.countIcon.frame = frame; 
     self.count.frame = frame; 

     background = [UIImage imageNamed:@"Folders_Badge_Count.png"]; 
     self.countIcon.image = [background stretchableImageWithLeftCapWidth:2 topCapHeight:2]; 

     self.count.hidden = NO; 
     self.countIcon.hidden = NO; 
    } 

    /* Code for _starred is similar to code for _count */ 
} 

です。

セルは、選択された新しいのViewControllerは、ナビゲーションスタックにプッシュされていて、私が持っているときにこれは、それが常にどのように見えるべきか(そしてそれは、セルを選択する前にどのように見えるか)

Before going back

ですこのビューコントローラをポップして戻って、これは見た目です。

After going back

アップデート2:

私は、セルのプロトタイプでImageViewのを保ったが、伸縮性の画像にそのサイズとそのイメージを設定し、コードをコメントアウトしています。ラベルの背景色を赤色に変更して、ラベルの正確なサイズを確認しました。サイズは正しいようです。

これは私の- (void)updateIndicatorsCount:andStarred:メソッドです。バックビューコントローラから起こった後

Before pushing

:別のビューコントローラにプッシュする前に

- (void)updateIndicatorsCount:(int)_count andStarred:(int)_starred 
{ 
    UIFont *badgeFont = [UIFont systemFontOfSize:11.0]; 

    // UIImage *background; 

    _count = 1000; 

    if (_count > 0) 
    { 
     self.count.text = [NSString stringWithFormat:@"%i", _count]; 

     CGSize size = [self.count.text sizeWithFont:badgeFont]; 

     CGRect frame = self.countIcon.frame; 
     frame.size.width = size.width; 
     frame.origin.x = 320 - 30 - size.width; 
     self.count.frame = frame; 
     self.count.hidden = NO; 
     self.count.backgroundColor = [UIColor redColor]; 

     /* DON'T DO ANYTHING TO THE IMAGE */ 
     /* 
     background = [UIImage imageNamed:@"Folders_Badge_Count.png"]; 
     self.countIcon.hidden = NO; 
     self.countIcon.image = [background stretchableImageWithLeftCapWidth:2 topCapHeight:2]; 
     self.countIcon.frame = frame; 
     */ 
    } 
} 

After going back

+0

ここでコードを表示できますか?そして、どのような方法でそれが行われていますか? – Jim

+0

スクリーンショットも役に立ちます。 – occulus

+0

@Jimコードを表示するための回答を更新しました。 – simonbs

答えて

0

あなたcellForRowatindexpathそれはあなたのを解決するには、このコードを使用します問題

​​
関連する問題