gpt4 book ai didi

ios - 在 UITableView 单元格中加载数组

转载 作者:行者123 更新时间:2023-11-29 02:45:20 25 4
gpt4 key购买 nike

我想在查看 detailController 后使用 UIScrollView 左右滑动分页。

首先,main.m:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
OZDetailViewController *detailViewController = [[OZDetailViewController alloc] initWithNibName:@"OZDetailViewController" bundle:nil];
detailViewController.arrDetailNews = [_arrNews objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];

OZDetailViewController *arrNewsAll = [[OZDetailViewController alloc] initWithNibName:@"OZDetailViewController" bundle:nil];
arrNewsAll.allNewsArray = _arrNews;
[self.navigationController pushViewController:arrNewsAll animated:YES];
}

当我在 tableviewcell 中选择内容时,arrDetailNews 可以在方法 viewDidLoad()cellForRowAtIndexPath() 中加载.但是 arrNewsAll 无法在方法 cellForRowAtIndexPath() 中加载。

这是我的detailViewController.h:

@property (nonatomic, copy) NSArray *allNewsArray;

detailViewCOntroller.m:

@synthesize allNewsArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor clearColor];
NSLog(@"dataArray: %@", allNewsArray);
}

- (int)numberINSectionsInTableView: (UITableView *)tableView
{
return 1;
}

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"dataArrayCell: %@", allNewsArray);

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"New Cell"];
}

if(indexPath.row != self.arrDetailNews.count-1){
UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
line.backgroundColor = [UIColor whiteColor];
[cell addSubview:line];
}

tableView.allowsSelection = NO;
cell.textLabel.text = [NSString stringWithFormat:@"%u", indexPath.row];

if (indexPath.row==0) {
cell.textLabel.text = @"1st";
}
if (indexPath.row==1) {
cell.textLabel.text = @"2nd";
}
if (indexPath.row==2) {
cell.textLabel.text = @"3rd";
}
return cell;
}

如果 allNewsArray 可以加载到 cellForRowAtIndexPath() 中,我可以继续下一步使用 UIScrollView 进行分页。请注意,numberOfRowsInSection 我设置为 4 因为我需要 4 行(自定义 View )。

最佳答案

假设您从 xib/storyboard 正确设置了委托(delegate)和数据源导出,您仍然需要指定每个部分的行数(或部分数)。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return allNewsArray.count;
}

或者,节数的方法是:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

关于ios - 在 UITableView 单元格中加载数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25213624/

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