Tableview并向其添加按钮

时间:2011-12-22 16:12:07

标签: iphone objective-c

我有两个问题。

1。)我有一个tableView,它将有超过100行(数据)。但我一次只能显示10条记录。当用户向下滚动并在第10行下方时,我想添加一个按钮,其中显示load the next 10 recordsload the previous 10 records。我如何添加这些按钮?注意:这些按钮仅在向下滚动到表格视图的最后一条记录时出现(我在iTunes中也找到了类似的例子)

2。)我需要这两个按钮具有箭头形状(As a Navigation button)

如何完成以编程方式。 (我不会使用界面构建器来创建它)

3 个答案:

答案 0 :(得分:1)

使用这些步骤和代码。

1)创建Button

2)UIButton的背景图片

3)添加目标(getNext10List)

4)创建页脚视图

5)在单元格中添加视图。

    UIButton *pagingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    pagingButton.frame = CGRectMake(0, 5, 316, 35);
    pagingButton.backgroundColor = [UIColor clearColor];
    UIImage *buttonImageNormal = [UIImage imageNamed:@"next10Holder@2x.png"];
    [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];

答案 1 :(得分: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

在控制器的-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;方法中,返回行数+ 1(对于带按钮的附加行)。在-(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提供正确的实施。

答案 2 :(得分:0)

如果是第11行,则将子视图上的按钮添加到单元格的contentView。