Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Add Custom Cell Randomly in UITableView

I have a UITableView which is populated with an NSArray. The array is sorted alphabetically in my table with alphabetized headers.

I am adding a custom cell randomly in my table at every 10th row.

The issue I have is that the custom cell is layering over actually data, so it's not being inserting within the array.

How can I solve this?

Here's what I am doing to insert the custom cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row % 10 == 10-1) {
        CustomAdCell *cell = (CustomAdCell *)[tableView dequeueReusableCellWithIdentifier:@AdCell];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@CustomAdCell owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        return cell;
    }
    else {

        static NSString *cellIdentifier = @cellIdentifier;
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        // Configure the cell...
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }

        NSString *key = [[[self indexedMembers] allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)][indexPath.section];

        self.indexedMembersArray = ((NSMutableArray *)[self indexedMembers][key]);

        MemberInfo *currentMember = self.indexedMembersArray[indexPath.row];

        cell.textLabel.text = [NSString stringWithFormat:@%@ %@", currentMember.firstName, currentMember.lastName];

        return cell;
}

Replies

  • RodoxydRodoxyd FrancePosts: 12 *
    edited July 2016
    Hi,

    You need to tweak the index to get the right value in array.
    You’re inserting an ad every 10 row of each section, starting at row 9 (first row is 0).
    You could have wrote:
    if ((indexPath.row + adOffset) % adFrequency == 0)
    
    with adFrequency = 10 and adOffset = 1 as static const.

    All you have to do is to retrieve the right index in array.
    NSInteger realIndex = indexPath.row - ((indexPath.row + adOffset)/adFrequency);
    MemberInfo *currentMember = self.indexedMembersArray[realIndex];
    

    Don’t forget to tweak the method numberOfRowsInSection :
    - (NSInteger)tableView:(UITableView *)tableView
            numberOfRowsInSection:(NSInteger)section {
        NSString *key = self.keys[section];
        NSArray *indexedMembersArray = self.indexedMembers[key];
        NSInteger membersCount = [indexedMembersArray count];
        return membersCount + (membersCount+adOffset)/adFrequency;
    }
    

    by the way :

    You should have an array of keys as property, and compute it only once in viewDidLoad:
    self.keys = [[self.indexedMembers] allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare: )];
    

    but indexedMembersArray should be local, as it should be used only in your cellForRowAtIndexPath method.
    The complete method looks like this :
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        if ((indexPath.row + adOffset) % adFrequency == 0) {
            CustomAdCell *cell = (CustomAdCell *)[tableView dequeueReusableCellWithIdentifier:@AdCell];
            if (cell == nil)
            {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@CustomAdCell owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            return cell;
        }
        else {
            static NSString *cellIdentifier = @cellIdentifier;
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            
            // Configure the cell...
            if (!cell) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            
            NSString *key = self.keys[indexPath.section];
            NSArray *indexedMembersArray = self.indexedMembers[key];
            NSInteger realIndex = indexPath.row - ((indexPath.row + adOffset)/adFrequency);
            MemberInfo *currentMember = indexedMembersArray[realIndex];
            cell.textLabel.text = [NSString stringWithFormat:@" %@ %@", currentMember.firstName, currentMember.lastName];
            return cell;
        }
    }
    

    There is a little more work to do if you want one ad every 10 rows cross section, but it can be done too.
Sign In or Register to comment.