iOS

A 3 post collection


setSelected:(BOOL)selected执行多次的问题

 •  Filed under iOS

呐 就是自定义Cell的时候 实现文件会有这个

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {}

我在这个里面写了

CarDetailTableViewController *OneCarDetailVC = [[CarDetailTableViewController alloc] init];
    OneCarDetailVC.CarID = CarIDLabel.text;
    [[self viewController].navigationController pushViewController:OneCarDetailVC animated:YES];

结果我发现在iPhone模拟器上一般不会出现什么奇怪的现象
但是在iPad上 几乎百分百要出现NavigationController在点击cell后 快速Push两次同一个OneCarDetailVC 然后就会有奇奇怪怪的BAD_EXC_ACCESS
开NSZombie后Log出的是

[CarDetailTableViewController respondsToSelector:]: message sent to deallocated instance 0x7ff794556400

然后Stackoverflow上这个说明了问题...

Since cells are reused when you scroll through a large table view, the table view has to keep the list of selected cells separate. Not only that, but whenever it reuses a cell it has to set its selected property, because it may be using an old, invalid selected state from a previous incarnation.

所以说 就是从:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {  
[super setSelected:selected animated:animated];
if (selected == YES) {
    CarDetailTableViewController *OneCarDetailVC = [[CarDetailTableViewController alloc] init];
    OneCarDetailVC.CarID = CarIDLabel.text;
    [[self viewController].navigationController pushViewController:OneCarDetailVC animated:YES];
}
}

变为

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {  

//多加一行这个
if (self.selected == selected) return;

[super setSelected:selected animated:animated];
if (selected == YES) {
    CarDetailTableViewController *OneCarDetailVC = [[CarDetailTableViewController alloc] init];
    OneCarDetailVC.CarID = CarIDLabel.text;
    [[self viewController].navigationController pushViewController:OneCarDetailVC animated:YES];
}
}

使用MFMailComposeViewController发邮件

 •  Filed under iOS

因为担心有API格式改了但是我不知道的情况 目前在做的软件里面设置了一个无法解析就提示发邮件的功能 因此学习了下MFMailComposeViewController

准备:
添加MessageUI.framework

#import <MessageUI/MessageUI.h>

Delegate:

@interface FirstTabListTableViewController ()<MFMailComposeViewControllerDelegate>

实现:

//这个SendParseErrorEmail是自定义的
- (void)SendParseErrorEmail
{
    if ([MFMailComposeViewController canSendMail])
    { // 用户已设置邮件账户
        // 邮件服务器
        MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
        // 设置邮件代理
        [mailCompose setMailComposeDelegate:self];
        
        // 设置邮件主题
        [mailCompose setSubject:@"报告软件错误"];
        
        // 设置收件人
        [mailCompose setToRecipients:@[@"邮箱地址"]];
        
        /**
         *  设置邮件的正文内容
         */
        NSString *emailContent = @"请填写:\niOS系统版本:\n手机软件版本:\n详细描述:\n我们将尽快解决,谢谢反馈!";
        // 是否为HTML格式
        [mailCompose setMessageBody:emailContent isHTML:NO];

        // 添加附件 这里我的日志叫做ErrorLog
        NSData *txt=[NSKeyedArchiver archivedDataWithRootObject:self.ErrorLog];
        [mailCompose addAttachmentData:txt mimeType:@"text/plain" fileName:@"Log.txt"];
        
        // 弹出邮件发送视图
        [self presentViewController:mailCompose animated:YES completion:nil];
        
    }
    else
    //没有登录邮箱的话 就弹我博客
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.justzht.com"]];
    }
        
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled: // 用户取消编辑
            NSLog(@"Mail send canceled...");
            break;
        case MFMailComposeResultSaved: // 用户保存邮件
            NSLog(@"Mail saved...");
            break;
        case MFMailComposeResultSent: // 用户点击发送
            NSLog(@"Mail sent...");
            break;
        case MFMailComposeResultFailed: // 用户尝试保存或发送邮件失败
            NSLog(@"Mail send errored: %@...", [error localizedDescription]);
            break;
    }
    
    // 关闭邮件发送视图
    [self dismissViewControllerAnimated:YES completion:nil];
}

iOS开发的一些奇技淫巧

 •  Filed under iOS, XCode

来自cocoachina

1.令TableView不显示没内容的Cell

self.tableView.tableFooterView = [[UIView alloc] init];

2.自定义leftBarbuttonItem 同时保留左滑返回手势

self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

3.解决ScrollView不能在viewController划到顶

self.automaticallyAdjustsScrollViewInsets = NO;

4.点击self.view就让键盘收起

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}

5.像safari一样滑动的时候隐藏navigationbar

navigationController.hidesBarsOnSwipe = Yes

6.tableview里cell的小对勾的颜色改成别的颜色

_mTableView.tintColor = [UIColor redColor];