iphone - UITableviewcell 的 NSMutableArray 数据源

标签 iphone ios xcode nsmutablearray

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

[_tableView reloadData];

我有一个 UITableview,我使用一个 NSMutable 字符串数组作为数据源,然后它工作正常。但是现在我想在这个数组中添加另一个字符串,就像上面那样点击一个按钮但是一旦我触摸按钮然后应用程序崩溃并给出

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

我如何做到这一点,我的 TableView 如下所示,请帮助我

- (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);
             }

最佳答案

如果您的数据源数组是 messageContentArray 并且它已经被实例化,那么您可以简单地这样做

- (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;
}

关于iphone - UITableviewcell 的 NSMutableArray 数据源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11108361/

相关文章:

iphone - 如何访问 iOS 4.0+ 相机拍摄的照片中的照片 EXIF?

ios - 在 UILabel 中的文本之间添加粗体文本

ios - XCode 失去了语法高亮的能力

ios - 是否可以在 swift 项目中将 Storyboard 构建的 ViewController 实例化为导入的 Objective-C 类?

Xcode:ld:找不到 -lAFNetworking 的库

iphone - UIButton 在点击时崩溃

iphone - 以编程方式设置 UIBarButtonItem 的位置

iphone - Objective-C : Check for Empty Array

ios - Flurry -retransmitNotSentBlocks 崩溃

UITableViewCell 的 iOS 商店选择?