> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Groups

> Scrollable list of the groups in your app, with group-type badges, member counts, live search, filters, selection and item callbacks.

`CometChatGroups` lists the groups in your app. Each row shows the group's avatar with a
badge for private and password-protected groups, its name and its member count. A tap
opens a group the user is in and joins a public one first. The list paginates as you scroll,
updates in real time and searches the server as you type in its search field.

![CometChatGroups showing groups with type badges and member counts](https://mintcdn.com/cometchat-22654f5b/pWDsskjSUHgot30F/images/ui-kit/ios/v6/docs-overview-groups.png?fit=max\&auto=format\&n=pWDsskjSUHgot30F\&q=85\&s=42c72c6d78fc26863e1eb90bfbd135d5)

***

## Where It Fits

`CometChatGroups` is the screen a user opens to find a group to chat in, or the picker a
"share to groups" flow embeds. It hands you the selected `Group` through `onItemClick`; open
your message screen with it.

```
CometChatGroups   ← you are here
        │ onItemClick(group)
        ▼
   your message screen
```

Both toolkits read from the same store, so the SwiftUI and UIKit lists can never disagree
about *what* to show.

***

## Quick Start

> [tab] SwiftUI
>
> ```swift theme={null}
> import CometChatSwiftUI
>
> struct GroupsScreen: View {
>     var body: some View {
>         CometChatGroups(
>             onItemClick: { group in openChat(with: group) }
>         )
>     }
> }
> ```
>
> The list draws a system navigation bar with a large title, so it brings its own
> `NavigationStack`. If your screen already has one, pass `embedsNavigationStack: false`.
> The search field is in the bar by default; pass `hideSearchBox: true` to leave it out.

> [tab] UIKit
>
> ```swift theme={null}
> import CometChatUIKit
>
> let groups = CometChatGroups()
>     .set(onItemClick: { group, indexPath in openChat(with: group) })
> navigationController?.pushViewController(groups, animated: true)
> ```
>
> `CometChatGroups` is a `UIViewController`. The screen uses large titles, so present it in a
> `UINavigationController`. The search field is in the navigation item by default;
> `setSearchBoxVisibility(false)` takes it out.

That is a working list: it fetches the first page and paginates as you scroll.
`onItemClick` hands you a `CometChatSDK.Group`: one the user is a member of, or a
password-protected one they have not joined yet — check `hasJoined`.

Prerequisites: the Kit initialised with `CometChatSession.initialize`, a user logged in, and
the package added. See [Getting Started](https://www.cometchat.com/docs/ui-kit/ios/v6/getting-started.md).

***

## What a Tap Does

Outside selection mode a tap depends on the group.

| The group is | A tap |
| - | - |
| one the user has joined | calls `onItemClick` |
| public, not joined | joins it under a "Joining Group" alert, then calls `onItemClick` with the joined group |
| password-protected, not joined | calls `onItemClick` with the group, its `hasJoined` still `false` |
| private, not joined | does nothing |

If the join fails `onError` is called and nothing else happens: there is no alert and no
error screen. A successful join is announced on the Kit's event bus as a member-joined event,
so the Kit's other components follow.

***

## Filtering Groups

Two builders: one for browsing, one used while a search is active.

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatGroups(
>     groupsRequestBuilder: GroupsRequest.GroupsRequestBuilder(limit: 30).set(joinedOnly: true),
>     searchRequestBuilder: GroupsRequest.GroupsRequestBuilder(limit: 30)   // search every group
> )
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let groups = CometChatGroups()
>     .set(groupsRequestBuilder: GroupsRequest.GroupsRequestBuilder(limit: 30).set(joinedOnly: true))
>     .set(searchRequestBuilder: GroupsRequest.GroupsRequestBuilder(limit: 30))
> ```

> [!NOTE]
>
> Both builders are used from the first page. With no search builder, a search runs on your
> list builder with the keyword added, and the keyword is taken off again afterwards. Giving
> the list a different list builder after it has loaded replaces the list with that builder's
> first page.

A search runs 0.6 s after the last keystroke and shows one page of results in place of the
list. Clearing the field shows the loaded list again without a request.

### Filter Recipes

| Goal | Builder |
| - | - |
| Joined groups only | `.set(joinedOnly: true)` |
| By tag | `.set(tags: ["sales"])` |
| With tags in the response | `.withTags(true)` |
| Larger pages | `.set(limit: 50)` |

***

## Actions and Events

### Callback Methods

> [tab] SwiftUI
>
> | Prop | Signature | When |
> | - | - | - |
> | `onItemClick` | `(Group) -> Void` | a joined group or a password group is tapped, or a public group has just been joined by a tap; in selection mode, every tap |
> | `onItemLongClick` | `(Group) -> Void` | a row is long-pressed |
> | `onSelection` | `(Set<String>) -> Void` | a row is selected or deselected |
> | `onSelectedItemProceed` | `([Group]) -> Void` | the selection bar's tick is tapped |
> | `onOptionSelected` | `(Group, MenuOption) -> Void` | a row option is chosen |
> | `onSearchTextChange` | `(String) -> Void` | the search field's text changes |
> | `onBack` | `() -> Void` | the back button is tapped |
> | `onLoad` | `([Group]) -> Void` | the loaded groups change |
> | `onEmpty` | `() -> Void` | the list becomes empty |
> | `onError` | `(CometChatError) -> Void` | a page, a search, a refresh or a join fails |

> [tab] UIKit
>
> | Setter | Signature | When |
> | - | - | - |
> | `set(onItemClick:)` | `(Group, IndexPath) -> Void` | a joined group or a password group is tapped, or a public group has just been joined by a tap; in selection mode, every tap |
> | `set(onItemLongClick:)` | `(Group, IndexPath) -> Void` | a row is long-pressed |
> | `set(onSelection:)` | `([Group]) -> Void` | a row is selected or deselected |
> | `set(onSelectedItemProceed:)` | `([Group]) -> Void` | the selection bar's tick is tapped |
> | `set(onLoad:)` | `([Group]) -> Void` | the loaded groups change |
> | `set(onEmpty:)` | `() -> Void` | the list becomes empty |
> | `set(onError:)` | `(CometChatException) -> Void` | a page, a search, a refresh or a join fails |
> | `set(onBack:)` | `() -> Void` | the back button is tapped |
>
> All but `onSelectedItemProceed` also reach a `CometChatGroupsDelegate`, which is usable from
> Objective-C. The back button is hidden by default; shown, it calls `onBack` and does not pop
> by itself unless no handler is set.

> [!WARNING]
>
> Setting `onItemLongClick` **replaces** the options menu: a long press calls it instead of
> opening the row's options.

`onLoad` is called with the loaded list — not a search's result — each time it changes.

`onError` is called for every failure. The error state is shown when nothing is loaded, and
also when the **next page** fails with rows on screen — it takes their place. A failed
refresh or search with rows on screen leaves them there. **Retry** reloads the first page. A
failed join is reported to `onError` and shows nothing.

### SDK Events (Real-Time, Automatic)

No wiring required — the store listens and the list updates itself.

| Event | Effect |
| - | - |
| Someone joins or is added | a private group appears at the top when it is the logged-in user; any other group's row updates |
| Someone leaves | a private group is removed when it is the logged-in user; otherwise the row updates |
| Someone is kicked or banned, or a scope changes | the group's row updates — it stays when the logged-in user is the one banned |
| Someone is unbanned | the group is added when it is the logged-in user and it is not listed; otherwise the row updates |
| A group is created elsewhere in the Kit | it is inserted at the top |
| A group is deleted elsewhere in the Kit | its row is removed |
| The user leaves a group elsewhere in the Kit | a private group is removed; any other is marked not joined |
| A member joins, is kicked or banned, or ownership changes, elsewhere in the Kit | the group's row updates |
| Connection restored | the first page is fetched again and replaces the list |

The first page is also fetched again, and replaces the list when it arrives, on
pull-to-refresh. The list does not reload when the screen re-appears. Removing the last
group leaves a blank list: the empty state is for the server answering with no groups, or a
search finding none.

***

## Functionality

> [tab] SwiftUI
>
> | Prop | Type | Default |
> | - | - | - |
> | `store` | `GroupsStore?` | a new store |
> | `searchText` | `Binding<String>?` | internal state |
> | `selection` | `Binding<Set<String>>?` | internal state |
> | `groupsRequestBuilder` | `GroupsRequest.GroupsRequestBuilder?` | 30 per page |
> | `searchRequestBuilder` | `GroupsRequest.GroupsRequestBuilder?` | the list builder |
> | `style` | `CometChatGroupsStyle` | `.init()` |
> | `title` | `String?` | "Groups" |
> | `hideToolbar`, `hideTitle`, `hideSearchBox`, `hideGroupType` | `Bool` | `false` |
> | `hideBackIcon`, `hideSeparator` | `Bool` | `true` |
> | `hideLoadingState`, `hideEmptyState`, `hideErrorState` | `Bool` | `false` |
> | `searchPlaceholderText` | `String?` | "Search" |
> | `.cometChatSearchScopes(_:selection:)` | modifier: `[String]`, `Binding<Int>` | no scope buttons |
> | `searchKeyword` | `String?` | `nil` — sets the field and searches |
> | `selectionMode` | `SelectionMode` | `.disabled` |
> | `initialSelectedGroups` | `[Group]?` | `nil` — in `.single` mode only the first is taken |
> | `embedsNavigationStack` | `Bool` | `true` |
> | `imageSource` | `any ImageSource` | the shared cached loader |
>
> `hideToolbar` hides the navigation bar, and the search field with it. `hideToolbarSeparator`,
> `passwordGroupIcon` and `privateGroupIcon` draw nothing; set the badge's glyphs through
> `itemStyle.statusIndicatorStyle`.

> [tab] UIKit
>
> | Method | Does |
> | - | - |
> | `setSeparatorVisibility(_:)`, `setGroupTypeVisibility(_:)`, `setSearchBoxVisibility(_:)`, `setBackIconVisibility(_:)`, `setToolbarVisibility(_:)`, `setTitleVisibility(_:)` | shows or hides a part |
> | `set(searchKeyword:)` | puts a keyword in the field and searches |
> | `refreshList()` / `fetchData()` | reloads the first page |
> | `refreshStyle()` | re-applies `style` after you change it |
>
> The same switches are properties: `hideGroupType`, `hideSearch`, `hideBackButton`,
> `hideNavigationBar`, `hideLoadingState`, `hideErrorView` and `disableEmptyState`.
> `hideBackButton` and `hideSeparator` are `true` by default; `hideSearch` is `false`.
>
> > [!NOTE]
> >
> > `setTitleVisibility(false)` blanks the title, and `setTitleVisibility(true)` does not
> > bring it back — call `set(title:)`.

***

## Custom View Slots

> [tab] SwiftUI
>
> Every slot is available as an initialiser argument and as a modifier that cascades to every
> `CometChatGroups` below it. Supply both and the initialiser argument wins.
>
> ```swift theme={null}
> CometChatGroups(
>     subtitleView: { group in AnyView(MyGroupSubtitle(group)) },
>     trailingView: { group in AnyView(MyJoinedBadge(group)) }
> )
> ```
>
> ```swift theme={null}
> .cometChatGroupLeadingView { group in MyGroupAvatar(group) }
> ```
>
> | Slot | Replaces | Modifier |
> | - | - | - |
> | `itemView` | the whole row | `.cometChatGroupItemView { group, isSelected in … }` |
> | `leadingView` | the avatar and its badge | `.cometChatGroupLeadingView { … }` |
> | `titleView` | the name | `.cometChatGroupTitleView { … }` |
> | `subtitleView` | the member count | `.cometChatGroupSubtitleView { … }` |
> | `trailingView` | nothing — it adds a trailing column | `.cometChatGroupTrailingView { … }` |
> | `loadingView` | the 30-row shimmer | `.cometChatGroupsLoadingView { … }` |
> | `emptyView` | the empty state | `.cometChatGroupsEmptyView { … }` |
> | `errorView` | the error state; receives `retry` | `.cometChatGroupsErrorView { retry in … }` |
> | `overflowMenu` | nothing — the bar's trailing item | `.cometChatGroupsOverflowMenu { … }` |
>
> Slot closures receive a `ChatGroup` — the row's value type (`id`, `name`, `type`,
> `membersCount`, `hasJoined`, `scope`, `ownerUID`).

> [tab] UIKit
>
> ```swift theme={null}
> let groups = CometChatGroups()
>     .set(subtitle: { group in MyGroupSubtitleView(group) })
>     .set(trailingView: { group in MyJoinedBadgeView(group) })
> ```
>
> | Setter | Replaces |
> | - | - |
> | `set(listItemView:)` | the whole row |
> | `set(leadingView:)` | the avatar and its badge |
> | `set(titleView:)` | the name |
> | `set(subtitle:)` | the member count |
> | `set(trailingView:)` | nothing — it adds a trailing view |
> | `set(loadingView:)`, `set(emptyView:)`, `set(errorView:)` | the three state views — loading is a 30-row shimmer |
> | `listItemCellClass` | the cell class — subclass `CometChatGroupListItem` |
> | `setOverflowMenu(_:)` | nothing — the bar's trailing item |

***

## Menu Options

The list has no options of its own. Give it some and each row shows them twice: as trailing
swipe actions and as a long-press menu.

> [tab] SwiftUI
>
> ```swift theme={null}
> CometChatGroups(
>     options: { group in
>         [MenuOption(id: "mute", title: "Mute", systemImage: "bell.slash")]
>     },
>     addOptions: { group in
>         [MenuOption(id: "leave", title: "Leave", systemImage: "arrow.right.square", isDestructive: true)]
>     },
>     onOptionSelected: { group, option in handle(option, for: group) }
> )
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let groups = CometChatGroups()
>     .set(options: { group in
>         [CometChatGroupOption(id: "mute", title: "Mute", icon: UIImage(systemName: "bell.slash"),
>                               onClick: { group, indexPath in mute(group) })]
>     })
>     .add(options: { group in
>         [CometChatGroupOption(id: "leave", title: "Leave", isDestructive: true)]
>     })
> ```

`options` come first and the added options after them; both lists are shown.

***

## Common Patterns

### Join a password-protected group

A password group comes to `onItemClick` like any other: check `hasJoined`, ask for the
password and join through the store.

> [tab] SwiftUI
>
> ```swift theme={null}
> @State private var store = GroupsStore()
> @State private var asking: CometChatSDK.Group?
> @State private var password = ""
>
> CometChatGroups(
>     store: store,
>     onItemClick: { group in
>         if group.hasJoined { openChat(with: group) } else { asking = group }
>     }
> )
> .alert("Enter password", isPresented: Binding(get: { asking != nil }, set: { if !$0 { asking = nil } })) {
>     SecureField("Password", text: $password)
>     Button("Join") {
>         guard let group = asking else { return }
>         Task {
>             if let joined = await store.join(group, password: password) { openChat(with: joined) }
>         }
>     }
>     Button("Cancel", role: .cancel) {}
> }
> ```

> [tab] UIKit
>
> ```swift theme={null}
> let groups = CometChatGroups()
> groups.set(onItemClick: { [weak groups] group, indexPath in
>     guard !group.hasJoined else { openChat(with: group); return }
>     let alert = UIAlertController(title: group.name, message: "Enter the password", preferredStyle: .alert)
>     alert.addTextField { $0.isSecureTextEntry = true }
>     alert.addAction(UIAlertAction(title: "Join", style: .default) { _ in
>         let password = alert.textFields?.first?.text
>         Task { @MainActor in
>             if let joined = await groups?.groupsStore.join(group, password: password) { openChat(with: joined) }
>         }
>     })
>     alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
>     groups?.present(alert, animated: true)
> })
> ```
>
> The "Joining Group" alert has two override points: `showJoiningGroupAlert(for:)` and
> `hideJoiningGroupAlert(completion:)`.

The store's `join(_:password:)` returns the joined group, or `nil` after calling `onError` —
a wrong password is reported there and nowhere else.

### Scope buttons under the search field

> [tab] SwiftUI
>
> `.cometChatSearchScopes(_:selection:)` puts scope buttons under the search field, in the
> navigation bar, as a New Chat screen switches between Users and Groups. The rows scroll under
> the field and the buttons with the bar's soft edge, and the large title stays sharp.
>
> | Parameter | Type | What it does |
> | - | - | - |
> | `titles` | `[String]` | the buttons, in order (right to left in a right-to-left language); empty shows none |
> | `selection` | `Binding<Int>` | the selected button's index: a tap writes it, and setting it moves the highlight. An index outside `titles` highlights the nearest button |
>
> The buttons show whether or not the field is being searched, and they don't filter the
> search: the list only reports the selection, and what it means is up to you. The modifier
> applies to every list under it, so when your screen swaps one list for the other, the
> buttons stay in the bar with the right one selected.
>
> ```swift theme={null}
> struct NewChatView: View {
>     @State private var page = 0
>
>     var body: some View {
>         NavigationStack {
>             Group {
>                 if page == 0 {
>                     CometChatUsers(embedsNavigationStack: false)
>                 } else {
>                     CometChatGroups(embedsNavigationStack: false)
>                 }
>             }
>             .cometChatSearchScopes(["Users", "Groups"], selection: $page)
>         }
>     }
> }
> ```

> [tab] UIKit
>
> The UIKit list has no scope buttons. Host `CometChatUsers` and `CometChatGroups` under your
> own `UISegmentedControl` — see [New Chat](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-new-chat.md).

***

## Advanced Methods

### Selection

> [tab] SwiftUI
>
> ```swift theme={null}
> @State private var selected: Set<String> = []
>
> CometChatGroups(
>     selection: $selected,
>     selectionMode: .multiple,
>     onSelectedItemProceed: { groups in share(to: Set(groups.map(\.guid))) }
> )
> ```
>
> Tapping rows fills `selected`.

> [tab] UIKit
>
> ```swift theme={null}
> let picker = CometChatGroups()
> picker.selectionMode = .multiple
> picker.set(onSelectedItemProceed: { groups in share(to: groups) })
> ```
>
> `getSelectedGroups()` reads the selection at any time, `clearSelection()` empties it and
> `selectGroup(_:mode:)` changes it.

In selection mode a tap selects or deselects, and nothing is joined. If you set
`onItemClick` it is called as well, before the selection changes. `onSelection` is called
with the whole selection on **every** change. In `.single` mode a second tap on the selected
row deselects it. A `.multiple` selection has no limit.

Once something is selected the navigation bar shows the count with a cross and a tick. The
tick calls `onSelectedItemProceed` with the selected groups and leaves the selection and the
mode as they are. The cross clears the selection; the mode stays on, so the next tap selects
again.

### Store Access

> [tab] SwiftUI
>
> ```swift theme={null}
> @State private var store = GroupsStore()
>
> CometChatGroups(store: store, onItemClick: { group in openChat(with: group) })
>     .toolbar { Button("Reload") { Task { await store.refreshList() } } }
> ```
>
> `GroupsStore` also exposes `join(_:password:)`, `search(_:)`, `fetchGroups()`, `retry()` and
> the list operations `addItem`, `insertItem`, `removeItem`, `updateItem`, `moveItemToTop` and
> `clearItems`.

> [tab] UIKit
>
> The view controller's store is `groupsStore`. The list methods on the view controller change
> the listed groups directly: `add(group:)`, `insert(group:at:)`, `update(group:)` and
> `remove(group:)`.

### Subclassing

> [tab] SwiftUI
>
> `CometChatGroups` is a `View` struct. Customise it with its slots, modifiers and style.

> [tab] UIKit
>
> These override points are `open`: `defaultSetup()`, `setupStyle()`, `styleSearchBar()`,
> `showEmptyView()`, `showErrorView()`, `showLoadingView()`,
> `updateNavigationBarTitleWithCount(_:)`, `tickButtonTapped()`, `crossButtonTapped()`,
> `showJoiningGroupAlert(for:)`, `hideJoiningGroupAlert(completion:)`,
> `onSearch(text:)`, `reloadData()`, `configureOptions(for:indexPath:)`,
> `configureMenu(for:indexPath:)` and the table view's data-source and delegate methods.
>
> ```swift theme={null}
> final class ShareToGroups: CometChatGroups {
>     override func defaultSetup() {
>         super.defaultSetup()
>         selectionMode = .multiple
>     }
>
>     override func updateNavigationBarTitleWithCount(_ count: Int) {
>         navigationItem.title = count == 0 ? "Share to groups" : "\(count) selected"
>     }
> }
> ```

***

## Style

> [tab] SwiftUI
>
> ```swift theme={null}
> var style = CometChatGroupsStyle()
> style.backgroundColor = Color(.systemGroupedBackground)
> style.itemStyle = GroupListItemStyle(titleColor: .indigo, avatarStyle: .init(size: 48))
> style.retryButtonCornerRadius = 20
>
> CometChatGroups(style: style)
> ```
>
> Unset properties fall through to the theme, so a style carries only what you change.
>
> | Property | Styles |
> | - | - |
> | `backgroundColor`, `strokeColor`, `strokeWidth`, `cornerRadius` | the screen |
> | `navigationBarBackgroundColor`, `navigationBarItemsTintColor` | the navigation bar and its buttons |
> | `itemStyle.titleColor` / `titleFont`, `subtitleColor` / `subtitleFont` | the row's two lines |
> | `itemStyle.avatarStyle` | the avatar's size, colours and font |
> | `itemStyle.statusIndicatorStyle` | the private / password badge |
> | `itemStyle.checkBox…` | the selection checkbox |
> | `itemStyle.selectedBackgroundColor` | a selected row's fill — none by default |
> | `itemStyle.separatorColor` / `separatorHeight` | the separator, shown with `hideSeparator: false` |
> | `emptyState…`, `errorState…`, `retryButton…` | the state views and Retry |
> | `loadingStateSurfaceColor` | the shimmer |
>
> > [!NOTE]
> >
> > On iOS 26 the navigation bar is Liquid Glass and the Kit leaves it to the system. Below
> > iOS 26, or with Reduce Transparency on, the bar is filled with the list's background.
> > Setting `navigationBarBackgroundColor` fills it everywhere.
>
> The title, the back button's glyph and the search field belong to the system's navigation
> bar; the style properties for them have no effect.

> [tab] UIKit
>
> ```swift theme={null}
> let style = GroupsStyle()
> style.listItemTitleTextColor = .systemIndigo
> style.privateGroupImageBackgroundColor = .systemTeal
> style.retryButtonCornerRadius = 20
>
> let groups = CometChatGroups().set(style: style)
> ```
>
> Set `CometChatGroups.style` once to change the default for every instance.
>
> > [!NOTE]
> >
> > `navigationBarTintColor` fills the navigation bar. Left unset, the bar is filled with the
> > list's background — except on iOS 26, where the bar is Liquid Glass and the Kit leaves it
> > to the system.

***

## Next Steps

> [card] [Group Members](https://www.cometchat.com/docs/ui-kit/ios/v6/group-members.md)
>
> List and manage the members of a group

> [card] [Message List](https://www.cometchat.com/docs/ui-kit/ios/v6/message-list.md)
>
> Show the messages of the group a tap opens

> [card] [Group Chat](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-group-chat.md)
>
> Create, join and manage groups

> [card] [Component Styling](https://www.cometchat.com/docs/ui-kit/ios/v6/component-styling.md)
>
> Style every component


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.