【问题标题】:Need to create different sections in UITableView需要在 UITableView 中创建不同的section
【发布时间】:2014-02-24 21:34:44
【问题描述】:

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

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

我需要在我的 UITableView 中创建 2 个不同的部分,每个数组一个部分。第一个数组的部分将称为“您通讯录中使用此应用的用户”,然后第二个数组的部分将称为“这些人还没有使用该应用”。

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

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

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

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

-(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;
}

【问题讨论】:

标签: ios objective-c arrays uitableview


【解决方案1】:

您应该实现 UITableViewDataSource 协议的 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;
}

【讨论】:

    【解决方案2】:

    要创建两个部分,您需要实现 – numberOfSectionsInTableView:。它是一个 UITableViewDataSource 协议方法。然后您可以使用if (indexPath.section == 0) {...} else if (indexPath.section == 1) {...} 区分– tableView:cellForRowAtIndexPath: 中的两个部分。在此之后,您可以实现- tableView:titleForHeaderInSection: 为每个部分指定标题。

    请注意,您的 tableView 的样式必须为 UITableViewStyleGrouped。表格视图具有 UITableViewStyle 类型的属性样式:

    typedef enum {
       UITableViewStylePlain,
       UITableViewStyleGrouped
    } UITableViewStyle;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 2011-08-26
      • 1970-01-01
      • 2012-02-28
      相关资源
      最近更新 更多