iOS实现搭建聊天页面的实例代码

由于工作需要,需要用到ios聊天页面,在网上搜了半天没有想要的,果断自己写一个,发个笔记

功能分析,模仿QQ聊天页面

输入框失去第一响应的情况:

1:点击页面

2:下滑页面

输入框成为第一响应的情况:

1:开始输入

2:上滑页面最底部

控制器

//
// WDPersonMessageDetailVC.m
// WestDevelopment
//
// Created by wangtao on 2017/6/23.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageDetailVC.h"
#import "WDPersonMessageDetailCell.h"
#import "WDPersonMessageFooterCell.h"
#import "WDPersonMessageDetailModel.h"

#import <IQKeyboardManager.h>

@interface WDPersonMessageDetailVC ()

@property (nonatomic, weak) WDPersonMessageFooterCell *textfieldView;

@end

@implementation WDPersonMessageDetailVC

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
  CGFloat contentOffsetY = scrollView.contentOffset.y;

//  页面下滑,并且输入框还是第一响应的时候,控制器要失去第一响应
  if (contentOffsetY > 10) {
    if (self.textfieldView.isFirst) {
      [self clickSelf];
    }
  }

  //  页面上滑,控制器成为第一响应
  if (contentOffsetY < - 10) {
    self.textfieldView.isFirst = YES;
  }
}

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];

//  关闭IQ键盘
  [IQKeyboardManager sharedManager].enable = NO;
  [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}

- (void)viewWillDisappear:(BOOL)animated
{
  [super viewWillDisappear:animated];
  [self.view endEditing:YES];
}

- (void)viewDidDisappear:(BOOL)animated
{
  [super viewDidDisappear:animated];

  [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  [IQKeyboardManager sharedManager].enable = YES;
}

- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)loadView
{
  UIScrollView *view = [[UIScrollView alloc] init];
  view.frame = CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight);
  self.view = view;

}

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.

  [IQKeyboardManager sharedManager].enable = NO;
  [IQKeyboardManager sharedManager].enableAutoToolbar = NO;

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

//  旋转tableView
  self.tableView.transform = CGAffineTransformMakeScale (1, -1);
  self.tableView.tableHeaderView.transform = CGAffineTransformMakeScale (1, -1);
  self.tableView.tableFooterView.transform = CGAffineTransformMakeScale (1, -1);

  self.view.backgroundColor = WTHexColor(0xeaeaea);
  self.tableView.backgroundColor = WTHexColor(0xeaeaea);
  self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0);

  [self.tableView registerClass:[WDPersonMessageDetailCell class] forCellReuseIdentifier:WDPersonMessageDetailCellID];
  [self.tableView registerClass:[WDPersonMessageFooterCell class] forHeaderFooterViewReuseIdentifier:WDPersonMessageFooterCellID];

  [self.tableView wt_addTapTarget:self action:@selector(clickSelf)];

  [self addFooter];

}
//键盘弹出时把消息列表tableView的高度设为(屏幕高度 - 输入框高度 - 键盘高度),同时输入框上移;
//键盘消失时再把tableView的高度设为(屏幕高度 - 输入框的高度),同时输入框下移。
//这样可以完美解决聊天列表的上面的消息无法显示问题和键盘遮挡问题。
- (void)keyboardWillShow:(NSNotification*)notification
{
  // 0.取出键盘动画的时间
  CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  // 1.取得键盘最后的frame
  CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  // 2.计算控制器的view需要平移的距离
  CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;

  // 3.执行动画
  NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];

  WTWS(weakSelf);
  dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    [UIView animateWithDuration:duration animations:^{
      weakSelf.tableView.frame = CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight - keyboardFrame.size.height - 64);
      weakSelf.inputView.transform = CGAffineTransformMakeTranslation(0, transformY);

    }];
  });

}

- (void)keyboardWillHide:(NSNotification*)notification
{
  CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  [UIView animateWithDuration:duration animations:^{
    self.tableView.frame = CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight);
    self.view.transform = CGAffineTransformIdentity;

  }];

}

//失去第一响应
- (void)clickSelf
{
  [[NSNotificationCenter defaultCenter] postNotificationName:kMessageState object:@(YES)];
}

- (void)addHeader
{
  __unsafe_unretained __typeof(self) weakSelf = self;
  self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
    [weakSelf loadData];
  }];

  [self.tableView.mj_header beginRefreshing];
}

//关闭下拉和上拉控件的文字展示
- (void)addFooter
{
//  [self addHeader];
  [self loadData];

  MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

  [footer setTitle:@"" forState:MJRefreshStateIdle];
  [footer setTitle:@"" forState:MJRefreshStatePulling];
  [footer setTitle:@"" forState:MJRefreshStateRefreshing];
  [footer setTitle:@"" forState:MJRefreshStateWillRefresh];
  [footer setTitle:@"" forState:MJRefreshStateNoMoreData];

  self.tableView.mj_footer = footer;

}

- (void)loadData
{
  self.page = 1;

  NSDictionary *par = @{
             kToken :     [WTAccount shareAccount].token,
             kUserId :     [WTAccount shareAccount].uid,
             kCurrentPage :  @(self.page),
             kFriendId :    self.friendId,
             };

  [WDNetwork postkMyMessageDetailPhoneWithParameters:par modelClass:[WDPersonMessageDetailModel class] responseBlock:^(id dataObject, NSError *error) {
    if (!error && [[dataObject class] isSubclassOfClass:[NSArray class]]) {
      NSArray* reversedArray = [[dataObject reverseObjectEnumerator] allObjects];
      self.dataArray = [NSMutableArray arrayWithArray:reversedArray];
      [self.tableView reloadData];
      self.page ++;
      if ([dataObject count] < 20) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
      } else {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
      }
    } else {
      [self.tableView.mj_header endRefreshing];
      [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
  }];
}

- (void)loadMoreData
{
  NSDictionary *par = @{
             kToken :     [WTAccount shareAccount].token,
             kUserId :     [WTAccount shareAccount].uid,
             kCurrentPage :  @(self.page),
             kFriendId :    self.friendId,
             };

  [WDNetwork postkMyMessageDetailPhoneWithParameters:par modelClass:[WDPersonMessageDetailModel class] responseBlock:^(id dataObject, NSError *error) {
    if (!error && [[dataObject class] isSubclassOfClass:[NSArray class]]) {
      NSArray* reversedArray = [[dataObject reverseObjectEnumerator] allObjects];
      [self.dataArray addObjectsFromArray:reversedArray];
      [self.tableView reloadData];
      self.page ++;
      if ([dataObject count] < 20) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
      } else {
        [self.tableView.mj_footer endRefreshing];
      }
    } else {
      [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }
  }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
  return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
  return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  WDPersonMessageFooterCell *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:WDPersonMessageFooterCellID];
  self.textfieldView = footer;
  footer.contentView.transform = CGAffineTransformMakeScale (1, -1);

  WTWS(weakSelf);
  footer.clickSenderText = ^(NSString *text) {
    NSDictionary *par = @{
               kToken :     [WTAccount shareAccount].token,
               kUserId :     [WTAccount shareAccount].uid,
               kComment :    text,
               kFlag :      @(11),
               kFriendId :    weakSelf.friendId,
               };
    [WDNetwork postkAddCommentPhoneWithParameters:par modelClass:[NSNull class] responseBlock:^(id dataObject, NSError *error) {
      if (!error && ([[dataObject objectForKey:kCode] integerValue] == 200)) {
        [weakSelf loadData];
        weakSelf.textfieldView.sendSucceed = YES;
      } else if (!error && [dataObject objectForKey:kMsg]) {
      }
    }];
  };

  footer.resignFirstRes = ^{
    weakSelf.tableView.frame = CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight - 64);
    weakSelf.view.transform = CGAffineTransformIdentity;
  };
  return footer;
}

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
  WDPersonMessageDetailModel *model = self.dataArray[indexPath.row];
  return model.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  WDPersonMessageDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:WDPersonMessageDetailCellID forIndexPath:indexPath];
  cell.model = self.dataArray[indexPath.row];
  cell.contentView.transform = CGAffineTransformMakeScale (1, -1);
  return cell;
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  // Get the new view controller using [segue destinationViewController].
  // Pass the selected object to the new view controller.
}
*/

@end

输入框 UITableViewHeaderFooterView

//
// WDPersonMessageFooterCell.h
// WestDevelopment
//
// Created by wangtao on 2017/6/26.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDBaseTVHeaderFooterView.h"

typedef void(^ClickSender_t)(NSString *text);

typedef void(^ResignFirstResponder)();

@interface WDPersonMessageFooterCell : WDBaseTVHeaderFooterView

@property (nonatomic, copy) ClickSender_t clickSenderText;
@property (nonatomic, copy) ResignFirstResponder resignFirstRes;

@property (nonatomic, assign) BOOL isFirst;
@property (nonatomic, assign) BOOL sendSucceed;

@end
//
// WDPersonMessageFooterCell.m
// WestDevelopment
//
// Created by wangtao on 2017/6/26.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageFooterCell.h"

@interface WDPersonMessageFooterCell () <UITextFieldDelegate>

@property (nonatomic, weak) UITextField *textField;
@property (nonatomic, weak) UIView *line;

@end

@implementation WDPersonMessageFooterCell

@synthesize isFirst = _isFirst;

- (void)setupAll
{
  self.contentView.backgroundColor = WTHexColor(0xf2f2f2);

  UITextField *textField = [[UITextField alloc] init];
  textField.backgroundColor = kWhiteColor;
  [self.contentView addSubview:textField];
  textField.delegate = self;
  self.textField = textField;
  textField.layer.cornerRadius = 3;
  textField.layer.masksToBounds = YES;

  textField.returnKeyType = UIReturnKeySend;

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(messageState:) name:kMessageState object:nil];

  UIView *line = [[UIView alloc] init];
  line.backgroundColor = WTHexColor(0xdddddd);
  [self.contentView addSubview:line];
  self.line = line;

}

- (void)messageState:(NSNotification *)noti
{
  NSInteger state = [[noti object] boolValue];
  if (state) {
    [self.textField resignFirstResponder];
    if (self.resignFirstRes) {
      self.resignFirstRes();
    }
  }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
  if (self.clickSenderText) {
    self.clickSenderText(self.textField.text);
  }
  return YES;
}

- (void)setIsFirst:(BOOL)isFirst
{
  _isFirst = isFirst;
  if (isFirst) {
    [self.textField becomeFirstResponder];
  } else {
    [self.textField resignFirstResponder];
  }
}

- (BOOL)isFirst
{
  if ([self.textField isFirstResponder]) {
    return YES;
  }
  return NO;
}

- (void)setSendSucceed:(BOOL)sendSucceed
{
  self.textField.text = @"";
}

- (void)layoutSubviews
{
  [super layoutSubviews];

  CGFloat padding = 10;
  self.textField.frame = CGRectMake(padding, padding, self.wt_width - padding * 2, self.wt_height - padding * 2);
  self.line.frame = CGRectMake(0, 0, self.wt_width, .5);
}

@end

消息cell

//
// WDPersonMessageDetailCell.m
// WestDevelopment
//
// Created by wangtao on 2017/6/23.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageDetailCell.h"
#import "WDPersonMessageDetailModel.h"

@interface WDPersonMessageDetailCell ()

@property (nonatomic, weak) UILabel *time;
@property (nonatomic, weak) UIImageView *icon;
@property (nonatomic, weak) UILabel *detail;

@property (nonatomic, weak) UIView *baseView;

@end

@implementation WDPersonMessageDetailCell

- (void)setupAll
{
  self.selectionStyle = UITableViewCellSelectionStyleNone;

  self.backgroundColor = WTHexColor(0xeaeaea);
  self.contentView.backgroundColor = WTHexColor(0xeaeaea);

  UILabel *time = [UILabel labelWithText:@""
                 textColor:WTHexColor(0xaaaaaa)
               textAlignment:NSTextAlignmentCenter
                   font:12
              backgroundColor:kClearColor];
  [self.contentView addSubview:time];
  self.time = time;

  UIImageView *icon = [[UIImageView alloc] init];
  [self.contentView addSubview:icon];
  icon.image = [UIImage imageNamed:kDefault];
  self.icon = icon;
  self.icon.layer.cornerRadius = 35 / 2;
  self.icon.layer.masksToBounds = YES;

  UIView *baseView = [[UIView alloc] init];
  [self.contentView addSubview:baseView];
  self.baseView = baseView;
  baseView.layer.masksToBounds = YES;
  baseView.layer.cornerRadius = 4;

  UILabel *detail = [UILabel labelWithText:@""
                  textColor:kBlackColor
                textAlignment:NSTextAlignmentLeft
                    font:13
               backgroundColor:kClearColor];
  [baseView addSubview:detail];
  self.detail = detail;
  detail.numberOfLines = 0;

}

- (void)setModel:(WDPersonMessageDetailModel *)model
{
  _model = model;

  if ([model.isShow isEqualToString:@"1"]) {
    self.time.text = model.addTime;
    self.time.hidden = NO;
    self.time.frame = CGRectMake(0, 0, kMainScreenWidth, 20);

  } else {
    self.time.text = @"";
    self.time.hidden = YES;
    self.time.frame = CGRectZero;

  }

  self.time.text = model.addTime;
  [self.icon wt_setImageWithUrlString:model.headImg placeholderString:@"me_icon"];
  self.detail.text = model.comment;

  if ([model.userId isEqualToString:[WTAccount shareAccount].uid]) {
    self.detail.textColor = kBlackColor;
    self.baseView.backgroundColor = kWhiteColor;

    self.icon.frame = CGRectMake(kPadding, self.time.wt_bottom + kPadding, 35, 35);
    self.baseView.frame = CGRectMake(self.icon.wt_right + kPadding, self.icon.wt_top, model.commentW, model.commentH);
    self.detail.frame = CGRectMake(kPadding, kPadding, model.commentW - kPadding * 2, model.commentH - kPadding * 2);

  } else {
    self.detail.textColor = kWhiteColor;
    self.baseView.backgroundColor = kHomeColor;

    self.icon.frame = CGRectMake(kMainScreenWidth - 35 - kPadding, self.time.wt_bottom + kPadding, 35, 35);
    self.baseView.frame = CGRectMake(self.icon.wt_left - kPadding - model.commentW, self.icon.wt_top, model.commentW, model.commentH);
    self.detail.frame = CGRectMake(kPadding, kPadding, model.commentW - kPadding * 2, model.commentH - kPadding * 2);

  }

}

@end

模型

//
// WDPersonMessageDetailModel.m
// WestDevelopment
//
// Created by wangtao on 2017/6/23.
// Copyright © 2017年 xikaijinfu. All rights reserved.
//

#import "WDPersonMessageDetailModel.h"

@implementation WDPersonMessageDetailModel

- (CGFloat)commentW
{
  if (_commentW == 0) {
    _commentW = [self.comment wt_calculateStringSizeWithFontOfSize:13 maxWidth:kMainScreenWidth / 2].width + 20;
  }
  return _commentW;
}

- (CGFloat)commentH
{
  if (_commentH == 0) {
    CGFloat textH = [self.comment wt_calculateStringSizeWithFontOfSize:13 maxWidth:kMainScreenWidth / 2].height;
    // 一行字体是15高,一行的情况就和头像一样高
    _commentH = (textH < 20) ? 35 : (textH + 20);
  }
  return _commentH;
}

- (CGFloat)height
{
  if (_height == 0) {
    _height = self.commentH + 20;
    if ([self.isShow isEqualToString:@"1"]) {
      _height += 20;
    }

  }
  return _height;
}

@end

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • iOS利用UIScrollView实现图片的缩放实例代码

    本文介绍了iOS利用UIScrollView实现图片的缩放实例代码,分享给大家: 第一步:添加scrollView到控制器中 UIScrollView *scrollView = [[UIScrollView alloc] init]; scrollView.frame = CGRectMake(40, 250, 300, 200); self.scrollView = scrollView; [self.view addSubview:scrollView]; 第二步:添加图片控件到scrol

  • React-Native实现ListView组件之上拉刷新实例(iOS和Android通用)

    在web应用中,上拉刷新加载更多,下拉刷新列表的操作非常常见,那么在React-Native中是如何实现呢,我们具体来看一下 ReactNative提供了RefreshControl下拉刷新组件,但是没有提供上拉刷新组件,上拉刷新在App中是很常用的. 今天我们来实现一个iOS和Android通用的上拉刷新功能. 下面简要介绍下我实现的思路. 思路: 1.常量定义: const moreText = "加载完毕"; //foot显示的文案 //页码 var pageNum = 1; /

  • IOS 开发之UILabel 或者 UIButton加下划线链接

    IOS 开发之UILabel 或者 UIButton加下划线链接          本文主要介绍了IOS中 UILable及UIButton的带下划线链接的实现方法及附有源码下载,大家开发IOS 应用有需要的可以参考下: 方法一: NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"查看所有中奖记录"]; NSRange strRange = {0,[str lengt

  • iOS输出手机系统版本号

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //手机系统版本 NSString* phoneVersion = [[UIDevice currentDevice] systemVersion]; NSLog(@"--手机系统版本--%@-----",phoneVersion); //手机型

  • iOS实现按钮点击选中与被选中切换功能

    其实这个没什么记的,初始化按钮给按钮分别设置选中时对图片和被选中时的图片,给按钮添加方法,在方法中实现三句话就OK了,下面直接看代码: 首先在.m中声明一个按钮 @property (nonatomic, strong) UIButton *selecBtn; //初始化按钮 UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom]; //给按钮设置图片 [button setBackgroundImage:[UIImage

  • IOS中多手势之间的冲突和解决办法

    IOS中多手势之间的冲突和解决办法 UIImageView默认是不支持交互的,也就是userInteractionEnabled=NO ,因此要接收触摸事件(手势识别),必须设置userInteractionEnabled=YES(在iOS中UILabel.UIImageView的userInteractionEnabled默认都是NO,UIButton.UITextField.UIScrollView.UITableView等默认都是YES). 在iOS中,如果一个手势A的识别部分是另一个手势

  • iOS实现搭建聊天页面的实例代码

    由于工作需要,需要用到ios聊天页面,在网上搜了半天没有想要的,果断自己写一个,发个笔记 功能分析,模仿QQ聊天页面 输入框失去第一响应的情况: 1:点击页面 2:下滑页面 输入框成为第一响应的情况: 1:开始输入 2:上滑页面最底部 控制器 // // WDPersonMessageDetailVC.m // WestDevelopment // // Created by wangtao on 2017/6/23. // Copyright © 2017年 xikaijinfu. All r

  • 用PHP+MySQL搭建聊天室功能实例代码

    下面,以一个简单的聊天室设计为例,介绍PHP+MySQL在网页开发中的应用 1.总体设计 1.1 构思与规划: 聊天室的基本原理,就是把每个连上同一网页的用户传送的发言数据储存起来,然后将所有的发言数据传给每一用户.也就是说,用数据库汇集每个人的发言,并将数据库中的数据传给每一个人就实现了聊天室的功能. 1.2 表设计 首先使用MySQL建立表chat用来储存用户的发言: 复制代码 代码如下: mysql> CREATE TABLE chat -> (chtime DATATIME, ->

  • 新手vue构建单页面应用实例代码

    本文介绍了新手vue构建单页面应用实例代码,分享给大家,具体如下 步骤: 1.使用vue-cli创建项目 2.使用vue-router实现单页路由 3.用vuex管理我们的数据流 4.使用vue-resource请求我们的node服务端 5.使用.vue文件进行组件化的开发 一.目录结构: 二.搭建项目 先安装 vue-cli: sudo npm install -g vue-cli 使用vue-cli构建初始化项目:vue init webpack project(创建webpack项目并下载

  • 微信小程序实现即时通信聊天功能的实例代码

    项目背景:小程序中实现实时聊天功能 一.服务器域名配置 配置流程 配置参考URL:https://developers.weixin.qq.com/miniprogram/dev/api/api-network.html 二.nginx中配置反向代理加密websocket(wss) upstream websocket{ hash $remote_addr consistent; server 127.0.0.1:9090 weight=5 max_fails=3 fail_timeout=30

  • 使用Ajax局部更新Razor页面的实例代码

    Razor功能非常强大,但是本身并不能做到无刷新,所以需要配合ajax使用 本文就做一个简单例子,实现Razor配合ajax做到局部刷新. 首先,我们创建一个MVC项目 让我们创建一个简单的Controller Book 然后对其添加一个视图,并且添加上一些简单的Html代码 @{ ViewBag.Title = "Index"; Layout = null; } <!DOCTYPE html> <html> <head> <meta http

  • Session过期后自动跳转到登录页面的实例代码

    最近做了一个项目其中有需求,要实现自动登录功能,通过查阅相关资料,打算用session监听来做,下面给大家列出了配置监听器的方法: 1.在项目的web.xml文件中添加如下代码: <!--添加Session监听器--> <listener> <listener-class> 监听器路径 </listener-class> </listener> 2.编写java类. public class SessionListener implements

  • SpringBoot创建JSP登录页面功能实例代码

    添加JSP配置 1.pom.xml添加jsp解析引擎 <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.s

  • iOS中 LGLAlertView 提示框的实例代码

    使用与iOS8 以后,只是把系统的UIAlertController进行了封装,省的每次用的时候要写很多的代码.封装后只需要一句代码即可 , deome 地址 :https://github.com/liguoliangiOS/LGLAlertView.git 上代码LGLAlertView.h: #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, LGLAlert

  • iOS 加载Bundle文件的实例代码

    废话不多说了,下面通过一段代码给大家介绍iOS 加载Bundle文件的方法,具体代码如下所示: - (NSString *)loadJsFile:(NSString *)fileName Type:(NSString *)type{ NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:type]; NSString *string1 = [NSString stringWithContentsOfFile

  • iOS自带动画效果的实例代码

     1.普通动画: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:2]; frame.origin.x += 150; [img setFrame:frame]; [UIView commitAnimations]; 2.连续动画(一系列图像): NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"myImage1.p

随机推荐