2011-12-22 14 views
0

私は2つの質問があります。テーブルビューとそれにボタンを追加

1.)私はtableViewを持っており、(データの)100以上の行があります。しかし、私は一度に10のレコードしか表示しません。ユーザーがスクロールして10行目の下に移動すると、load the next 10 recordsload the previous 10 recordsというボタンが追加されます。どのようにこれらのボタンを追加するのですか?注意:あなたは、テーブルビューの最後のレコードまでスクロールしたときにこれらのボタンにのみ表示されていることを、私は矢印の形(As a Navigation button)

に持っているものを2つのボタンを必要とする

2)(私もiTunesで同様の例を見つけました)

どうすればいいですか?はプログラムでです。 (私はこれを作成するためにインターフェイスビルダを使用しません)

+0

これらのボタンはどこにありますか? 10行目が表示された後に – Ariel

+0

と表示されます。 – Illep

+0

次に、これらのボタンを使ってもう1つの行を追加してください。 – Ariel

答えて

1

これらの手順とコードを使用します。

1)UIButton

3)ターゲットを追加するためにボタンを

2)背景画像を作成する(getNext10List)

4)を作成し、フッタビュー

5)にビューを追加するには、細胞。

UIButton *pagingButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    pagingButton.frame = CGRectMake(0, 5, 316, 35); 
    pagingButton.backgroundColor = [UIColor clearColor]; 
    UIImage *buttonImageNormal = [UIImage imageNamed:@"[email protected]"]; 
    [pagingButton setBackgroundImage:buttonImageNormal forState:UIControlStateNormal]; 
    [pagingButton addTarget:self action:@selector(getNext10List) forControlEvents:UIControlEventTouchUpInside]; 

    //create a footer view on the bottom of the tabeview 
    footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 316, 45)]; 
    [footerView addSubview:pagingButton]; 

    messageTableView.tableFooterView = footerView;  
    [[self view] addSubview:messageTableView]; 
+0

私はViewDidLoadメソッドでこれらのコードを追加する必要がありますか?私は2つのボタンを持つ必要があるので、別のボタンを作成してそれを 'messageTableView'に追加する必要がありますか?私は 'UINavigationButton'形状のボタンをここで取得する必要がありますが、どうすればいいですか? (これらの質問には申し訳ありません、私は初心者です) – Illep

+0

10行セルの読み込みを完了した後、このコードを追加してから、tableviewをリロードしてください。データのロードが完了したら、このコードを追加します。私はactualPageCountを教えてくれる旗があります。あなたがもうデータを持っていなければ、あなたのボタンとtablefotterviewを削除します。 – AAV

+0

データは 'cellForRowAtIndexPath'メソッドのセルに移入されるので、すべてのデータが一度設定されたら、あなたが指定したコードを追加しますか?どのような方法で私はそれを置くべきですか?( 'cellForRowAtIndexPath'の後に呼び出されるメソッドは何ですか?それに追加する必要がありますか?) – Illep

0

11行目の場合は、ボタンを押したサブビューをセルのcontentViewに貼り付けます。

+0

どうすればいいですか?表示するレコードが十分でない場合(レコードが3つある場合は、ボタンを4行目に追加する必要があります)可能であれば、サンプルコードを提供できますか? – Illep

1

カスタムUITableViewCellサブクラスが必要です。そのような何か:

.hファイル:

#define kBackButtonPressedNotification @"kBackButtonPressedNotification" 
#define kForwardButtonPressedNotification @"kForwardButtonPressedNotification" 

@interface ButtonsTableViewCell : UITableViewCell { 
@private 
    UIButton* _backButton; 
    UIButton* _forwardButton; 
} 
@end 

.mファイル:

@implementation ButtonsTableViewCell 
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if(self){ 
     _backButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     _backButton.frame = CGRectMake(10, 10, 100, 30); //put numbers that are good for you 
     [_backButton setImage:[UIImage imageNamed:@"someBackButtonImage.png"] forState:UIControlStateNormal]; 
     [_backButton addTarget:self action:@selector(backPressed) forControlEvents:UIControlEventTouchUpInside]; 
     [self.contentView addSubview:_backButton]; 

     _forwardButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     _forwardButton.frame = CGRectMake(10, 10, 100, 30); //put numbers that are good for you 
     [_forwardButton setImage:[UIImage imageNamed:@"someForwardButtonImage.png"] forState:UIControlStateNormal]; 
     [_forwardButton addTarget:self action:@selector(forwardPressed) forControlEvents:UIControlEventTouchUpInside]; 
     [self.contentView addSubview:_forwardButton]; 
    } 
    return self; 
} 

-(void)backPressed{ 
    [[NSNotificationCenter defaultCenter]postNotificationName:kBackButtonPressedNotification object:nil]; 
} 

-(void)forwardPressed{ 
    [[NSNotificationCenter defaultCenter]postNotificationName:kForwardButtonPressedNotification object:nil]; 
} 
@end 

行+ 1のあなたのコントローラの-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;メソッドの戻り値の数で(追加の行に対応するボタンを備えました)。 -(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;では、indexPath.rowを確認し、最後のときはそのカスタムセルのインスタンスを返します。そしてもちろん、あなたはのはこのように言わせて、これらの通知を登録する必要があります:

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourBackMethod) name:kBackButtonPressedNotification object:nil]; 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourForwardMethod) name:kForwardButtonPressedNotification object:nil]; 
} 

-(void)viewWillDisappear:(BOOL)animated{ 
    [super viewWillDisappear:animated]; 
    [[NSNotificationCenter defaultCenter]removeObserver:self]; 
} 

yourBackMethodyourForwardMethodのための適切な実装を提供することを忘れないでください。

関連する問題