gpt4 book ai didi

ios - 如何动态地制作具有多个部分的uitableview?

转载 作者:行者123 更新时间:2023-11-28 19:11:05 25 4
gpt4 key购买 nike

如何使用动态数组计数动态制作具有多个部分的 uitableview?

最佳答案

检查这个长代码,我想这会派上用场

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#pragma mark TableView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.words count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.words objectAtIndex:section]count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

// UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
// if (cell==nil) {
// cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
// }

//UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
//if (cell==nil) {
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
//}

cell.textLabel.text=[[self.words objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:@"This is detailed subtitle for %@.",cell.textLabel.text];
//cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;

NSString *imagePath=[[NSBundle mainBundle]pathForResource:@"images" ofType:@"jpeg"];
UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];
cell.imageView.image=image;


return cell;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [self.alphabets objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;{
return self.alphabets;
}

#pragma mark -
#pragma mark Default



- (void)viewDidLoad
{

self.alphabets=[NSMutableArray new];
for (char i=97; i<123; i++) {
[self.alphabets addObject:[[NSString stringWithFormat:@"%c",i]capitalizedString]];
}

self.words=[NSMutableArray new];
for (NSString *character in _alphabets) {
NSMutableArray *twoD=[NSMutableArray new];
for (int i=1; i<(rand()%10)+1; i++) { //fill randamly any number 1 to 10.
[twoD addObject:[NSString stringWithFormat:@"%@%d",character,i]];
}
[self.words addObject:twoD];
}

// NSLog(@"->%@",self.words);


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

关于ios - 如何动态地制作具有多个部分的uitableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15426321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com