Lazar K Lazar K - 5 months ago 18
Objective-C Question

Obj C - populate TableView with objects at specific indexes in array

I want to populate my TableView with objects from my array at specific indexes. I thought I could do this by looping an

line and adding the objects that I want to populate the TableView with (at the specific indexes) to a new array and then populating the tableView with the new array but at the end the new array is nil.

The first array is layout like this: email, name, key code, email, name, key code, email, name, key code, etc...... So i want to fill the second array with just the keycodes and then use the key code array and fill the table with it

This is what I have so far:


@property(nonatomic) NSArray *productList;
@property(nonatomic) NSMutableArray *productKeycodes;


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// Return the number of sections.
// If You have only one(1) section, return 1, otherwise you must handle sections
return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
// Return the number of rows in the section.
return [self.productList count] / 3;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

self.productKeycodes = [NSMutableArray new];

bool flag = true;
int index = 3;
int arraySize = [self.productList count];
int numberOfKeycodes = [self.productList count] / 3;

while (flag) {

self.productKeycodes = [self.productList objectAtIndex: index];

//[self.productKeycodes insertObject: self.productList atIndex: index];

if (arraySize > numberOfKeycodes) {
index = index + 3;
arraySize = arraySize - 3;
flag = false;

cell.textLabel.text = [NSString stringWithFormat:[self.productKeycodes objectAtIndex: indexPath.row]];

return cell;

Breakpoint data after loop is done:

self ProductListViewController * 0x7a2831c0
UIViewController UIViewController
_productListTableView UITableView * 0x7b30c400
_productList __NSArrayM * @"49 elements" 0x78ebe380
_productKeycodes NSMutableArray * nil 0x00000000
numberOfKeycodes int 16
index int 26
arraySize int 13
flag bool true

At get a
signal SIGABRT
error here:

#import <UIKit/UIKit.h>
#import "AppDelegate.h"

int main(int argc, char * argv[]) {
@autoreleasepool {
//The line below
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));//Here!
//The line above

is my logic wrong and/or is there a better way to do this? Thanks!


replace all code in tableView:cellForRowAtIndexPath: with this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    cell.textLabel.text = self.productList[indexPath.row * 3 + 2];
    return cell;