【问题标题】:NSMutable array to UITableview when button clicked单击按钮时 NSMutablearray 到表视图
【发布时间】:2025-12-06 14:15:01
【问题描述】:

我想在点击收件箱按钮时在 UITableView 中显示我的电子邮件标题,我的电子邮件标题存储在 NSMutable 数组中,

   -(IBAction)buttonInbox:(id)sender{

        NSLog(@"Inbox Button Clicked %@",buttonInbox);
        [self select:@"INBOX"];
        NSArray *lines = [self sendCommand:@"fetch 1:* (body[header.fields (from subject date)])"];



        //NSMutableArray *emaiArray= [[NSMutableArray alloc] init];
        NSMutableString *totalEmail= [NSMutableString stringWithString:@""];

        int counter = 0;
        int i =0;
        NSRange tmp;
        for(i=0;i<[lines count];i++)
        {

            NSString *line = [lines objectAtIndex:i];

            //NSLog(@" Burda %@" ,line);
            tmp = [line rangeOfString:@"Date:"];
            if( tmp.location != NSNotFound ) 
            {
                //NSLog(@" Date basildi %@" ,line);
                counter++;
                [totalEmail appendString:line ];

            }

            tmp = [line rangeOfString:@"From:"];
            if (tmp.location != NSNotFound ) {
                //NSLog(@" From basildi %@" ,line);
                counter++;
                [totalEmail appendString:line ];
            }

            tmp = [line rangeOfString:@"Subject:"];
            if (tmp.location != NSNotFound ) {
                //NSLog(@" Subject basildi %@" ,line);
                counter++;
                [totalEmail appendString:line ];
            }
            if (counter==3) {
                [emailList addObject:totalEmail];
                counter =0;
                NSLog(@"total Email %@" ,totalEmail);
                totalEmail = [NSMutableString stringWithString:@""];
            }
  }
}

到目前为止我已经尝试过了,但是当按钮单击事件时找不到任何指导方针,这无论如何都不会显示一个空的 tableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];

    }

    cell.textLabel.text = [emailList objectAtIndex: indexPath.row];

    return cell;
}

【问题讨论】:

    标签: iphone objective-c uitableview nsmutablearray


    【解决方案1】:

    尝试在buttonInbox: 选择器的末尾调用[[self tableView] reloadData]

    这将刷新 tableView。

    不过,我建议您查看 UITableView 的文档并了解如何使用 -(void) beginUpdates-(void) endUpdates,以便获得更好的视觉体验并让表格视图为传入的行设置动画。

    【讨论】:

    • 这不起作用是因为我的表视图声明为 UITableView *mytableView; @property (nonatomic, retain) IBOutlet UITableView *mytableView; 和我没有在我的 .m 文件中使用任何 mytableview 方法?
    • 我想通了,我忘记将委托拖到文件所有者以获取 tableview