0

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; ????

8
  • wheres the rest of your code? Commented May 26, 2014 at 9:48
  • add your text and image in different arry then show them in table by using tableView delegate method Commented May 26, 2014 at 9:49
  • 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. :-) Commented May 26, 2014 at 9:52
  • Do you use ARC? And, how do you declare text variable (strong,nonatomic) in Message object? Commented May 26, 2014 at 9:58
  • I think I use ARC. This is the default setting of XCODE5, iOS7, and I do not change anything Commented May 26, 2014 at 10:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.