0

I am working on a reminder app and in that i needed to show event name, event image and event time.All of them are getting values from diff array but problem is that if there are content of the first array is 2 and second is three then in No.of rows in a section if I specified return[timeArray count]; then app crashes.What to do??

here is my code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   // return [timeArray count];

   // return [imagesArray count];

    return [nameArrayP2 count];
}


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

    cell.timeLabel.text = [timeArray objectAtIndex:indexPath.row];
        cell.eventNameLbl.text =[nameArrayP2 objectAtIndex:indexPath.row];
        UIImage *image=[UIImage imageNamed:[imagesArray objectAtIndex:indexPath.row]];
        cell.imgView.image =image;

        return cell;
}

Updated Code 6 June:

 NSArray *timeA = [[NSUserDefaults standardUserDefaults]objectForKey:@"TimeArray"];
    timeArray =[[NSMutableArray alloc]init];
    [timeArray addObjectsFromArray:timeA];
    NSLog(@"%@",timeArray);


    NSArray *nameA = [[NSUserDefaults standardUserDefaults]objectForKey:@"nameArrayP2"];
    nameArrayP2 =[[NSMutableArray alloc]init];
    [nameArrayP2 addObjectsFromArray:nameA];
    NSLog(@"%@",nameArrayP2);


    NSArray *imageA =[[NSUserDefaults standardUserDefaults]objectForKey:@"images"];
    imagesArray =[[NSMutableArray alloc]init];
    [imagesArray addObjectsFromArray:imageA];
    NSLog(@"abhi%@",nameArrayP2);



    dataMutableArray = [[NSMutableArray alloc]init];

    NSDictionary *dataDict = [[NSDictionary alloc]initWithObjectsAndKeys:nameArrayP2,@"eventTitle", timeArray,@"eventTime",imagesArray, @"eventImage", nil];

     NSDictionary *dataDict1 = [[NSDictionary alloc]initWithObjectsAndKeys:nameArrayP2,@"eventTitle", timeArray,@"eventTime",imagesArray, @"eventImage", nil];
    //dataMutableArray =[NSMutableArray arrayWithArray:[dataDict allValues]];
    [dataMutableArray addObject:dataDict]; [dataMutableArray addObject:dataDict1];

   // [dataMutableArray addObject:dataDict];
    NSLog(@"All Data=%@",dataMutableArray);
  • I'm pretty sure that you can just change it when you need to load the different array, check if your TableView object has a numberOfRowsInSection property where you can assign the new value – JamesRGNT May 23 '17 at 06:38
  • @JamesRGNT i have to show all the content every time when content of this array mismatches in no.then error occurs. – iOSBeginner May 23 '17 at 06:41
  • I did not get, how could you get different count in the array. It should be same if you are keeping set of 3 (name, image, time) in respective array. But the best approach is without saving in array, you should take objects. – Janmenjaya May 23 '17 at 06:41
  • @Janmenjaya since some events don't have a image this error occurs.i am saving it buy using NSuserdefaults. can you plz tell me how to use objects??? I am beginner in iOS plz consider If it a silly question. Thanks ! – iOSBeginner May 23 '17 at 06:45

4 Answers4

0

You can create an object for the event, in which you can have property for name, image, time etc..,

Before reloading the tableView, create an object from the list of the array and save this event as an object in an array ad use the same for tableView.

Arasuvel
  • 2,971
  • 1
  • 25
  • 40
0

You should have a Custom Object which should be initialized using different dataArray and then in tableview datasource methods use the array of custom object

NSArray *eventArray = [self loadEventArray:timeArray withImage:imageArray withName:nameArray];

- (NSArray*) loadEventArray:(NSArray*) timeArray withImage:(NSArray*) imageArray withName:(NSArray*) nameArray {

NSMutableArray *eventArray = [[NSMutableArray alloc] init];

for(int anIndex = 0; anIndex < [nameArray count]; anIndex++){
    Event *event = [[Event alloc] init]; // Custom Object
    // Your logic to identify event and assign name
}

for(int anIndex = 0; anIndex < [imageArray count]; anIndex++){
    // Fetch the linked event from event Array
    // assign image
}

for(int anIndex = 0; anIndex < [timeArray count]; anIndex++){
    // Fetch the linked event from event Array
    // assign time
}

return eventArray;
}
Anuj Panwar
  • 683
  • 7
  • 10
0

You can simply create an Array of Dictionaries. Take property of NSMutableArray if you want to manipulate the array later for adding or removing the events.

NSMutableArray *dataMutableArray = [[NSMutableArray alloc]init];

You can create dictionary of data and add it inside your array. Something like this.

NSDictionary *dataDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"I am event title",@"eventTitle", @"I am image",@"eventImage",@"I am event time", @"eventTime", nil];

[dataMutableArray addObject:dataDict];

You can also create a model class which hold events data like

@property (strong, nonatomic) NSString *eventName;
@property (strong, nonatomic) NSString *eventTime;
@property (strong, nonatomic) NSString *eventDate;

//Inside your view controller
    EventData *eventObject = [[EventData alloc]init];
    eventObject.eventTime = @"I am event time";
    eventObject.eventImage = @"I am image";
    eventObject.eventTitle = @"I am event title";

    [dataMutableArray addObject:eventObject];

Below will be your tableview datasource methods

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



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
        {
//You can use any of the one approach 
//1)
            NSDictionary *dataDict = [dataMutableArray objectAtIndex:indexPath.row];

            cell.timeLabel.text = [dataDict valueForKey:@"eventTime"];
            cell.eventNameLbl.text =[dataDict valueForKey:@"eventTitle"];
            UIImage *image=[UIImage imageNamed:[dataDict valueForKey:@"eventImage"]];
            cell.imgView.image =image;

            return cell;

//2)    
        EventData *eventObject = [dataMutableArray objectAtIndex:indexPath.row];

       cell.timeLabel.text = eventObject.eventTime;
       cell.eventNameLbl.text = eventObject.eventTitle;
       UIImage *image=[UIImage imageNamed:eventObject.eventImage];
       cell.imgView.image =image;

       return cell;

    }
  • thanks for the answer.It solved my crash problem but now if one of my array contains the multiple data then it should display in the second row but it is showing only first value & only one row. – iOSBeginner Jun 06 '17 at 05:30
  • Data{ eventImage = ( "meeting-ico.png", "video-ico.png", "shopping-ico.png", "video-ico.png" ); eventTime = ( "07.45" ); eventTitle = ( meeting, video, shopping, video ); } like this.in this it supposed to return me four rows but it is only showing one – iOSBeginner Jun 06 '17 at 05:34
  • The above code I demonstrated for just one record. You have to create another dictionary or object with different value and add it to array and so on. – Dhaval Soni Jun 06 '17 at 05:40
  • can you give me the example for multiple??Since I tried it for multiple and no luck :( – iOSBeginner Jun 06 '17 at 06:33
  • Can you try this below code. NSDictionary *dataDict = [[NSDictionary alloc]initWithObjectsAndKeys:@"I am event title",@"eventTitle", @"I am image",@"eventImage",@"I am event time", @"eventTime", nil]; NSDictionary *dataDict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"I am event title",@"eventTitle", @"I am image",@"eventImage",@"I am event time", @"eventTime", nil]; [dataMutableArray addObject:dataDict]; [dataMutableArray addObject:dataDict1]; – Dhaval Soni Jun 06 '17 at 09:22
  • it is giving me error like this :- 'NSInvalidArgumentException', reason: '-[__NSDictionaryI length]: unrecognized selector sent to instance 0x60800026a540' – iOSBeginner Jun 06 '17 at 09:55
  • In which method is it giving error? Can you let me know the "dataMutableArray" length after adding those two dictionaries. – Dhaval Soni Jun 06 '17 at 10:08
  • In no. of rows in the section should be [dataMutableArray count]. – Dhaval Soni Jun 06 '17 at 10:17
  • I am doing the same. – iOSBeginner Jun 06 '17 at 10:22
  • Can you share your initialisation code please. I need to have a look at it. – Dhaval Soni Jun 06 '17 at 10:27
  • Try this code. for (int i = 0; i < nameArrayP2.count; i++) { NSDictionary *dataDict = [[NSDictionary alloc]initWithObjectsAndKeys:[nameArrayP2 objectAtIndex:i],@"eventTitle", [timeArray objectAtIndex:i],@"eventTime",[imagesArray objectAtIndex:i], @"eventImage", nil]; [dataMutableArray addObject:dataDict]; } – Dhaval Soni Jun 06 '17 at 10:41
  • Now it's not crashing right. Can you share your cellForRowAtIndexPath method code. – Dhaval Soni Jun 06 '17 at 11:13
  • NSDictionary *dataDict = [dataMutableArray objectAtIndex:indexPath.row]; cell.timeLabel.text = [dataDict valueForKey:@"eventTime"]; cell.eventNameLbl.text =[dataDict valueForKey:@"eventTitle"]; UIImage *image=[UIImage imageNamed:[dataDict valueForKey:@"eventImage"]]; cell.imgView.image =image; – iOSBeginner Jun 06 '17 at 11:16
  • I guess your user default data is empty that's why it is not displaying anything right now. Check whether nameArrayP2 is having any data inside it. – Dhaval Soni Jun 06 '17 at 11:18
  • firstly it is not showing any data nameArrayP2 but i changed now and there is one object in that but it is showing this msg in debug area :timed out after 0.012s (5060 5061); suspension count=0 (IOSuspensions: ) – iOSBeginner Jun 06 '17 at 11:28
  • hello mate, now it is showing the one value each but my time array has 2 values and it is showing only one value. – iOSBeginner Jun 06 '17 at 11:33
  • you need to store whole array of dictionaries inside user defaults. Current implementation is wrong because there is inconsistency in records for name, time and image. So you need to create an array and add whole dictionary the way i have created in my code and store that array inside user defaults. So it will have consistent count. So u need to change storing logic. – Dhaval Soni Jun 06 '17 at 12:13
  • okay.thanks for the help mate .I'll try that.Really appreciate your efforts :) – iOSBeginner Jun 06 '17 at 12:19
0

You should follow this way

Take one NSObject class say EventBO and take 2 NSString and 1 NSDate (or as per your requirement take other type) attribute like (eventName, eventImage, eventTime). NB: Keep the image name in the image property

#import <Foundation/Foundation.h>
@interface EventBO : NSObject

@property (nonatomic, strong) NSString *eventName;
@property (nonatomic, strong) NSString *eventImage;
@property (nonatomic, strong) NSDate *eventTime;

@end

In Your Controller:

Take Global mutable array say NSMutableArray *arrEvents

Allocate the array (preferably on viewDidLoad)

arrEvents = [[NSMutableArray alloc] init];

Then assign the values of Event (i.e eventName, eventTime and eventImage) in the object and add the object in the Global array

EventBO *objEvent = [[EventBO alloc]init];
objEvent.eventName = @"test";
objEvent.eventImage = @"test.png";
objEvent.eventTime = [NSDate date]; //You can put your time, I have set current date-time

[arrEvents addObject:objEvent]

Now use the array in the table to fetch the object and display data.

halfer
  • 19,824
  • 17
  • 99
  • 186
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43