1
NSString *myMessage=messageField.text;
[messageArray addObject:myMessage]];
messageContentArray=[[NSMutableArray alloc] initWithArray:messageArray];

[_tableView reloadData];

I have a UITableview and I used a NSMutable array of string as datasource then it works fine. But now I want to add another string in this array on click of a button as above but as soon as I touch the button then application crash and give

'NSInvalidArgumentException', reason: '-[NSCFString count]: unrecognized selector sent to instance 0x4e11cf0'

How I can do this my table view is as bellow please help me

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

                 return [messageContentArray count];
             }
#pragma mark -
#pragma mark UITableView Delegaates
static CGFloat padding = 20.0;
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
                 static NSString *CellIdentifier = @"messagesCellIdentifier";

                 SMMessageViewTableCell *cell = (SMMessageViewTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

                 if (cell == nil) {
                     cell = [[[SMMessageViewTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
                 }
                 NSString *usrid=[useridArray objectAtIndex:indexPath.row];
                NSData *imgData1 = (NSData*)[[NSUserDefaults standardUserDefaults] objectForKey:@"lisnerImage"];
                 UIImage *img1 = [UIImage imageWithData: imgData1];

                // CGRect imageFrame=CGRectMake(5,20,25,30);
                 //CGRect imageFrame1=CGRectMake(250,20,25,30);
                 //self.cellimage=[[[UIImageView alloc] initWithFrame:imageFrame] autorelease];
//               self.cellimage1=[[[UIImageView alloc] initWithFrame:imageFrame1] autorelease];
                 if ([usrid caseInsensitiveCompare:friend_Id] == NSOrderedSame) {

                     [cell.usrImage setFrame:CGRectMake(250, 
                                                           20, 
                                                           25, 
                                                          30)];
                     cell.usrImage.image = userImage.image;
                    // self.cellimage1.image=userImage.image;
//                    [cell.contentView addSubview:self.cellimage1];
                     NSString *messages1 = [messageContentArray objectAtIndex:indexPath.row];
                     CGSize  textSize = { 260.0, 10000.0 };
                     CGSize size = [messages1 sizeWithFont:[UIFont systemFontOfSize:13]
                                         constrainedToSize:textSize 
                                             lineBreakMode:UILineBreakModeWordWrap];
                     size.width += (padding/2);
                     cell.messageContentView.text = messages1;
                     cell.accessoryType = UITableViewCellAccessoryNone;
                     cell.userInteractionEnabled = NO;
                     UIImage *bgImage = nil;
                     bgImage = [[UIImage imageNamed:@"aqua.png"] stretchableImageWithLeftCapWidth:24  topCapHeight:15];

                     [cell.messageContentView setFrame:CGRectMake(45 , 
                                                                  20, 
                                                                  190, 
                                                                  size.height)];

                     [cell.bgImageView setFrame:CGRectMake(45, 
                                                           17, 
                                                           190, 
                                                           size.height+padding)];
                     cell.bgImageView.image = bgImage;
                 }else {
                     //self.cellimage.image=img1;
//                   [cell.contentView addSubview:self.cellimage];
                     [cell.usrImage setFrame:CGRectMake(5, 
                                                        20, 
                                                        25, 
                                                        30)];
                     cell.usrImage.image = img1;
                         NSString *messages1 = [messageContentArray objectAtIndex:indexPath.row];
                     CGSize  textSize = { 260.0, 10000.0 };
                     CGSize size = [messages1 sizeWithFont:[UIFont systemFontOfSize:13]
                                         constrainedToSize:textSize 
                                             lineBreakMode:UILineBreakModeWordWrap];
                     size.width += (padding/2);
                     cell.messageContentView.text = messages1;
                     cell.accessoryType = UITableViewCellAccessoryNone;
                     cell.userInteractionEnabled = NO;
                     UIImage *bgImage = nil;
                     bgImage = [[UIImage imageNamed:@"orange.png"] stretchableImageWithLeftCapWidth:24  topCapHeight:15];

                     [cell.messageContentView setFrame:CGRectMake(45, 20, 190, size.height)];

                     [cell.bgImageView setFrame:CGRectMake( 45, 
                                                           17, 
                                                           190, 
                                                           size.height+padding)];
                                         cell.bgImageView.image = bgImage;

                 }



                 return cell;
             }
             - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
                             }
             -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
                // return 65;

                 NSString *text = [messageContentArray objectAtIndex:[indexPath row]];

                 CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

                 CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];

                 CGFloat height = MAX(size.height+20, 44.0f);

                 return height + (CELL_CONTENT_MARGIN * 2);
             }
3
  • Where does messageArray come from? Which array are you using to determine how many rows you display? Which array are you reading from to get text for the cells? Commented Jun 19, 2012 at 19:58
  • Message array is a string. InitWithArray takes an array. That's your crash. Call it messageString so you won't be confused. Commented Jun 19, 2012 at 20:03
  • Sorry. misread your snippet. Please post your datasource methods - numberOfRows and cellForRow. Commented Jun 19, 2012 at 20:08

3 Answers 3

2

If your datasource array is messageContentArray and it is instantiated already, then you can simply do this

- (IBAction)clicked:(id)sender {
  NSString *myMessage = messageField.text;
  [messageContentArray addObject:myMessage];
  [_tableView reloadData];
}

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

    // Customize the appearance of table view cells.
-(UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *str =  [messageContentArray objectAtIndex:indexPath.row];
    [cell.textLabel str];

    return cell;
}
Sign up to request clarification or add additional context in comments.

1 Comment

you need a return on your tableView:numberOfRowsInSection: method.
0

The dataSource of UITableView is not an array, but an object (most often UIViewController), that implements UITableViewDataSource protocol. Here is the documentation.

1 Comment

He means it's the data structure supporting the data source.
-1

I would never use NSMutableArray for your datasource.

I would just do the following: (messageContentArray is now NSArray and thus immutable)

NSString *myMessage=messageField.text;
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:messageArray]; 
[tempArray addObject:myMessage];
self.messageContentArray = [NSArray arrayWithArray:tempArray];
[_tableView reloadData];

One thing that this error is saying, is the fact that you are trying to get count from NSString...so make sure wherever you are calling count it is on NSArray and not on NSString which does not implement this method.

9 Comments

Why would you never use NSMutableArray as a datasource? Seems like a better idea than building a duplicate array each time.
In all books and literature I read no-one used a NSMutableArray as a datasource...and I think when Jeff LaMarche does not use it I don't use it as well. With NSArray you are sure what you are getting, with NSMutableArray can change right under you which I do not think is good...
@Ladislav an NSMutableArray wont change under you if you know what you are doing...after all you are the one in control of it..
@Daniel, I know it will not, but still it is much easier to make a mistake then with immutable array. And if more than one people use your code, someone else can make that mistake for you :)
I'd say it's a lot easier to make a mistake when you have to constantly update an NSArray any time you want to add anything to your datasouce. Not to mention the overhead of constantly copying and making new arrays
|

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.