0
votes

My UITableView contains messages (a message has a text and an image). At the beginning, my UITableView is empty:


@property (strong, nonatomic) NSMutableArray *messages;
@property (strong, nonatomic) IBOutlet UITableView* tableView;

- (void)viewDidLoad{  
    self.messages = [[NSMutableArray alloc] init];
}

The UITableViewDelegate, and UITableViewDataSource is connected in the IB. When I get a message, I want to show it on the UITableView as follow:

- (void)didReceiveMessage:(NSNotification *) notification{

    //- extract message
    NSDictionary* userInfo = notification.userInfo;
    Message* msg = [userInfo objectForKey:kNotificationMessage];

    //- update table
    [self.messages addObject:msg];
    [self.tableView reloadData];
    [self scrollTableViewToBottom];
}

At this point, my Messages mutable array contains exactly the message. However, when reloading the UITableView, the message seems to point to another address.

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

    Message *message = [self.messages objectAtIndex:indexPath.row];     
    cell.textLabel.text = message.text;
    cell.imageView.image = [message getImage];
}

the message is not nil, but message.text is nil. I doubt that the userInfo is release when getting out of function didReceiveMessage. So, that's why the message is not nil, but point to an address that contains nothing. What I tried to do is retain or copy the message before adding it to the NSMutableArray messages like:

- (void)didReceiveMessage:(NSNotification *) notification{

    //- extract message
    NSDictionary* userInfo = notification.userInfo;
    Message* msg = [userInfo objectForKey:kNotificationMessage];
    Message* copyMsg = [msg copy];

    //- update table
    [self.messages addObject:copyMsg];
    [self.tableView reloadData];
    [self scrollTableViewToBottom];
}

But I am afraid that doing such thing will make the program to be leak. Do you have any solution? Or explain me why?

More info:

my Message class looks like this:

@interface Message : NSObject

@property (nonatomic, assign) NSString *text;

@end

Do I need to change it to @property (nonatomic, retain) NSString* text; ????

1
wheres the rest of your code?Simon McLoughlin
add your text and image in different arry then show them in table by using tableView delegate methodGajendra Rawat
I got some problem related to text format while posting question. While the question was not completed, you could answer it. I really admire you. :-)chipbk10
Do you use ARC? And, how do you declare text variable (strong,nonatomic) in Message object?nmh
I think I use ARC. This is the default setting of XCODE5, iOS7, and I do not change anythingchipbk10

1 Answers

1
votes

In Message object, you declare text variable:

@property (nonatomic, assign) NSString *text

This is the root cause why textis nil when you get it's value. If you set value

NSString *s = @"SomeValue"; Message.text=s;

s variable is released after that and your text will be released too. So, you should declare text variable is strong to keep its value.