如何将一个按钮添加到tableview的最后一行?
我无法找到解决方案如何在UITableViewCell
中添加此“加载更多”按钮。如何将一个按钮添加到tableview的最后一行?
像 “更多结果...” 选项,在这个应用程序
希望有人能帮助我...
而不是普通的内容,使得你的上单元格UIButton。当用户单击它时,用新信息更新数据源(不要忘记更新每个部分中的行数),并在tableview上调用reloadData。始终将按钮保持在最底部的单元格中。
如何更新数据源完全取决于您的实施。
要将按钮添加到一个UITableViewCell你可以的cellForRowAtIndexPath做到这一点:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == dataForCells.count + 1){ // Assume dataForCells is an NSArray holding your data. Initially it should hold 10 objects, then after clicking this button it will be 20, 30, etc. as you load data in load10MorePressed. However, this depends on your implementation. You could be storing your data in some other way.
static NSString *CellIdentifier = @"LoadMoreCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectInset(cell.frame, 5, 5);
[button setTitle:@"Load 10 More" forState:UIControlStateNormal];
[button addTarget:self action:@selector(load10MorePressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
} else {
static NSString *CellIdentifier = @"NormalCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Setup your normal data cells here
}
}
但是你要确保你有一个名为load10MorePressed方法:
-(IBAction)load10MorePressed:(id)sender{
NSMutableArray *temp = [NSMutableArray arrayWithArray:dataForCells];
for (int x=0; x<10; x++){
[temp addObject:[Create new objects here. Depends on your implementation, probably downloading from someplace...]];
}
self.dataForCells = temp;
[self.tableView reloadData];
}
最后,不要” t忘记返回桌面视图中的单元格数量:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == 0){
return dataForCells.count + 1;
} else {
return 0;
}
}
感谢您的回复,但我是iPhone开发新手,所以我我无法在Tableview中放置该按钮。就像我在表格中有10行那样,那么我如何在第11行放置“loadmore”选项。你可以给我一些链接或一些代码。 – user755278
我已经更新了我的回复,以显示如何创建单元格并加载更多数据。 – arsenius
我以这种方式从XML获取数据(我在每个页面获得10条新闻,有10个页面,我通过在api中更改“%@”(占位符)的值来获取这些消息,所以我我再次无法做到这一点,我真的非常感谢你的回复我工作的这个解决方案你给我,但我再次无法忍受这个问题(我有一个API像: - http://www.xyz.com /?page =%@我将这个占位符“%@”的值改为1到10,因为每个页面有10个新闻,每个页面有10条新闻,所以我的问题是我能够解析显示在表格中,但是我如何设置10个新闻加载更多按钮将显示)... – user755278
As far a我的知识你可以这样做。
把按钮加载多了。当用户点击buttonAction中的按钮时,取一个BOOL变量,然后使用[tableview reloadData];然后在numberofrowsintableview方法中检查BOOL变量。如果是,则返回(Ex: )10 + previousrows.Pass数据也
我明白你的问题: - 你要做的是在表视图下面添加一个活动指标。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text=[self.array objectAtIndex:indexPath.row];
if (indexPath.row ==[array count] -1) { //this method will get called when you will scroll to the bottom of your table view
[self sendRequestMethod];
}
else {
//NSLog(@"not nearest row");
//[self remove];
}
return cell;
}
-(void)sendRequestMethod
{
[self.newTableView setUserInteractionEnabled:NO];
[indicator startAnimating];//make sure you start your indicator now perform what you want to do IN my case I am sending a request to server
NSString *str = [NSString stringWithFormat:@"http://www.google.com"];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setDidFinishSelector:@selector(responseSuccedMethod:)];
[request setDidFailSelector:@selector(responseFailedMethod:)];
[networkQueue addOperation: request];
[networkQueue go];
}
-(void)responseSuccedMethod:(ASIHTTPRequest *)req
{
[self.newTableView setUserInteractionEnabled:YES];
[indicator stopAnimating];
[newTableView reloadData];
}
谢谢你的回复,但我是新的iPhone开发,所以我无法将该按钮放在Tableview中。就像如果我在表中有10行那么我该如何放置该“loadmore”选项在第11排。你可以给我一些链接或一些代码。 – user755278
也许做到这一点更简单的方法是采用tableView:viewForFooterInSection:
和tableView:heightForFooterInSection
方法,并与“加载更多结果”按钮返回视图。
-(id)init
{
self = [super init];
if(self) {
// create a footer view so that we can add it later when we need to
loadMoreView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
UIButton* moreButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[[moreButton titleLabel] setText:@"Load More Results"];
[moreButton addTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
[loadMoreView addSubview:moreButton];
// other init code here
}
return self
}
-(UIView*)tableView:(UITableView*)tv viewForFooterInSection:(NSInteger)s
{
return loadMoreView;
}
-(CGFloat)tableView:(UITableView*)tv heightForFooterInSection:(NSInteger)s
{
return [loadMoreView frame].size.height; // or 40 since that's what we set it to
}
我认为,这是一个更清洁的方法。但是你也需要为UIButton指定框架; moreButton.frame = CGRectMake(0,0,160.0,40.0); – fatih
你究竟想要做什么。当你向下滚动表格时,你是否想要这样做,然后加载更多出现,直到没有新的结果。 – Gypsa
我无法在Tableview中放置该按钮。就像我在表格中有10行那样,我怎样才能在第11行放置“loadmore”选项。你可以给我一些链接或一些代码。 – user755278
可能重复的[在TableView中加载更多选项?](http://stackoverflow.com/questions/6936968/load-more-option-in-tableview) –