簡體   English   中英

UITableview單元的NSMutable Array數據源

[英]NSMutable Array data source for UITableview cell

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

[_tableView reloadData];

我有一個UITableview,我使用NSMutable字符串數組作為數據源,然后工作正常。 但是現在我想在上面單擊按鈕時在此數組中添加另一個字符串,但是一旦我觸摸按鈕,應用程序就會崩潰並給出

'NSInvalidArgumentException',原因:'-[NSCFString count]:無法識別的選擇器已發送到實例0x4e11cf0'

我如何做到這一點我的表格視圖如下所示,請幫助我

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

UITableViewdataSource不是數組,而是一個實現UITableViewDataSource協議的對象(通常是UIViewController )。 這是文檔

我永遠不會將NSMutableArray用於您的數據源。

我只需執行以下操作:(messageContentArray現在為NSArray,因此是不可變的)

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

該錯誤表示的一件事是,您試圖從NSString中獲取計數的事實...因此,請確保無論您調用的計數是在NSArray上,而不是在沒有實現此方法的NSString上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM