gpt4 book ai didi

ios - 需要在 UITableView 中创建不同的部分

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

我有 2 个数组,其中包含人们的名字,例如 John 或 Steve。第一个数组包含在我的服务器数据库中并且已经在使用我的应用程序的人。第二个数组包含用户地址簿中的人员,但不在我的数据库中并且之前从未使用过我的应用程序。

现在当我在我的 UITableView 上显示这些名称时,它们都在一起并且没有分开。

我需要在我的 UITableView 中创建 2 个不同的部分,每个数组一个部分。第一个数组的部分将称为“地址簿中使用此应用程序的用户”,然后第二个数组的部分将称为“这些人尚未使用该应用程序”。

我基本上是在努力实现这张图片显示的效果:http://appdupe.com/wp-content/uploads/2013/11/snapchat-clone-script-add.png

请注意有一个名为“Snapchat 上的 friend ”的部分,然后是第二个名为“从联系人中邀请 friend ”的部分。

如何为我的 UITableView 创建这些相同的“部分”?

以下是我用来设置表格 View 的当前方法实现:

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


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

static NSString *cellIdentifier = @"SettingsCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


NSString *firstNameForTableView = [self.potentiaFriendsFirstNamesArray objectAtIndex:indexPath.row];

NSString *userNameForTableView = [self.potentiaFriendsUsernameArray objectAtIndex:indexPath.row];


[cell.textLabel setText:firstNameForTableView];
[cell.detailTextLabel setText:userNameForTableView];
return cell;
}

-(NSString*)stringBetweenString:(NSString*)start andString:(NSString *)end withstring:(NSString*)str
{
NSScanner* scanner = [NSScanner scannerWithString:str];
[scanner setCharactersToBeSkipped:nil];
[scanner scanUpToString:start intoString:NULL];
if ([scanner scanString:start intoString:NULL]) {
NSString* result = nil;
if ([scanner scanUpToString:end intoString:&result]) {
return result;
}
}
return nil;
}

最佳答案

您应该实现 UITableViewDataSource 协议(protocol)的 numberOfSectionsInTableView 和 sectionIndexTitlesForTableView 方法,并更改当前的 numberOfRowsInSection 和 cellForRowAtIndexPath。像这样:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [NSArray arrayWithObjects: @"Users from your address book that use this app", @"These people don't use the app yet", nil];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return [self.firstArrayOfUsers count];
} else {
return [self.secondArrayOfUsers count];
}
}

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

static NSString *cellIdentifier = @"SettingsCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

NSString* userName = nil;

if (indexPath.section == 0) {
username = [self.firstArrayOfUsers objectAtIndex:indexPath.row];
} else {
username = [self.secondArrayOfUsers objectAtIndex:indexPath.row];
}

[cell.textLabel setText:username];
return cell;
}

关于ios - 需要在 UITableView 中创建不同的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21999512/

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