Objective c 如何在UITableView上分步加载NSArray
当我单击“确定返回”按钮时,一个数组有三十个元素,然后它应该在TableView上显示二十个元素,在下一次单击后,我应该显示十个元素,依此类推。看起来你差不多到了 您需要使用UITableViewDataSource协议中的Objective c 如何在UITableView上分步加载NSArray,objective-c,uitableview,Objective C,Uitableview,当我单击“确定返回”按钮时,一个数组有三十个元素,然后它应该在TableView上显示二十个元素,在下一次单击后,我应该显示十个元素,依此类推。看起来你差不多到了 您需要使用UITableViewDataSource协议中的tableView:cellForRowAtIndexPath:。此方法有效地将数组中的正确索引链接到UITableViewCell e、 g.类似于: -(void)addMyButton { button = [UIButton buttonWithType:UIBu
tableView:cellForRowAtIndexPath:
。此方法有效地将数组中的正确索引链接到UITableViewCell
e、 g.类似于:
-(void)addMyButton
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod) forControlEvents:UIControlEventTouchUpInside];
button.tag = 1;
[button setTitle:@"Show More" forState:UIControlStateNormal];
button.frame = CGRectMake(110.0, 430.0, 100.0, 40.0);
[self.view addSubview:button];
}
-(void)viewDidLoad
{
[self addMyButton]; // Call add button method on view load
n=1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(n==1){
n++;
return 10;
}
else if(n==2){
n++;
return 20;
}
else
return 30;
}
-(void) aMethod
{
if(n>=3)
{
[button setTitle:@"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//[button setBackgroundColor:[UIColor brownColor]];
}
else{
// [button setTitle:@"Show More" forState:UIControlStateNormal];
// [button setBackgroundColor:[UIColor clearColor]];
}
NSLog(@"n==>> %d",n);
[tbl reloadData];
}
请尝试编辑的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];
}
cell.textLabel.text = [self.myArray objectAtIndex:indexPath.row];
return cell;
}
请尝试我的代码,如果我遗漏了任何符合您要求的内容,请告诉我。像这样尝试
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(n==1)
{
n++;
return 20; //edited from return 10 to return 20-- to return 20 elements for the first time
}
else if (n==2)
{
return 30; //Return 30 when click on view more
}
}
-(void) aMethod
{
if(n==2)
{
[button setTitle:@"Return" forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
//[button setBackgroundColor:[UIColor brownColor]];
}
else
{
n=1;
[button setTitle:@"Show More" forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor clearColor]];
}
NSLog(@"n==>> %d",n);
[tbl reloadData];
}
试试这个
#import "YourTableViewControllerClass.h"
@interface YourTableViewControllerClass ()
{
NSMutableArray * tableArray;
}
@end
@implementation TableViewController
- (void)viewDidLoad
{
[super viewDidLoad];
tableArray = [[NSMutableArray alloc]initWithArray:@[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"]];
}
-(IBAction)addMoreCells // button action to add more cells
{
[tableArray addObjectsFromArray:@[@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20"]];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return tableArray.count ;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = [tableArray objectAtIndex:indexPath.row];
return cell;
}
@end
您可以使用下面给出的伪代码检查功能
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
请重新格式化您的代码(并对您试图实现的目标添加更多解释)-这是不可读的。嘿?有一件事:你不应该在
行数部分增加n
<如果您试图实现更多的加载功能,那么code>aMethod
将是一个更好的地方。那么这个方法不是更好,请搜索更多答案&你会得到更好的方法。标题标签是什么?我对它进行了编辑,以显示一个标准的UITableViewCell实现。将“myArray”更改为您的NSArray属性。这是编写代码,但这不是问题的解决方案?这不是问题的解决方案。然后请尝试清楚地解释您的问题。是否每次单击按钮时都要显示下10项(或用当前项追加下10项)?用当前项追加下10个元素
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
return cell;
}