日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

IOS开发笔记(九)——IM聊天工具个人详情页面,自定义tableview的accessaryView

發(fā)布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 IOS开发笔记(九)——IM聊天工具个人详情页面,自定义tableview的accessaryView 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

中山大學(xué)數(shù)據(jù)科學(xué)與計算機(jī)學(xué)院本科生實驗報告

(2019年春季學(xué)期)

課程名稱IOS開發(fā)任課老師鄭貴鋒
年級16專業(yè)(方向)軟件工程(計算機(jī)應(yīng)用方向)
學(xué)號16340132姓名梁穎霖
電話13680473185Emaildic0k@qq.com
開始日期2019/5/12完成日期2019/5/15

一、實驗題目

IM聊天工具

二、實現(xiàn)內(nèi)容

  • 個人詳情頁面UI
  • 后端接口:獲取聊天消息記錄

三、實驗結(jié)果

聊天消息記錄

1.概述

為了實現(xiàn)聊天工具對話的消息傳遞,這里我們引入了序列號的概念,每一個消息都有它特定的序號,我們根據(jù)這個序列號來確定這個消息是否已經(jīng)發(fā)送,是否已經(jīng)被接受到,是否需要刪除等邏輯。

除此之外,為了實現(xiàn)多用戶間的對話,例如A->B, A->C, B->C,他們之間如何能找到屬于他們各自的信息呢?這里我們利用的是維護(hù)一個消息表,每一條消息都有發(fā)送用戶的id,接受用戶的id,內(nèi)容的id,然后再通過內(nèi)容的id在內(nèi)容表格中找具體的內(nèi)容。

2. URL設(shè)計

獲取用戶之間聊天記錄 /history/personal POST { to: {username} data: {} }

在Django的urls.py文件中表現(xiàn)為

urlpatterns = [path('personal', views.personal, name='personal'), ]

3. 具體實現(xiàn)

a.從消息表通過用戶名獲取聊天記錄
# 返回聊天記錄 def personal(request, t_username):response = {'state':'fail', 'msg':'no msg'}# 要在登錄狀態(tài)下if 'login_id' not in request.session:response['msg'] = 'no login'return HttpResponse(json.dumps(response), content_type = 'application/json')# 只允許GET方法if request.method != 'POST':response['msg'] = 'wrong method'return HttpResponse(json.dumps(response), content_type = 'application/json')# 查詢的用戶名為空if t_username == '':response['msg'] = 'miss username'return HttpResponse(json.dumps(response), content_type = 'application/json')# 已經(jīng)登錄, 所以拿取用戶信息cur_username = request.session['login_id']# 數(shù)據(jù)庫操作,查詢消息try:# 當(dāng)前用戶發(fā)送給對方的信息cur_to_t_msg = Msg.objects.filter(Username = t_username, From = cur_username)# 對方發(fā)送給當(dāng)前用戶的信息t_to_cur_msg = Msg.objects.filter(Username = cur_username, From = t_username)except Exception as e:response['msg'] = 'db error'return HttpResponse(json.dumps(response), content_type = 'application/json')else:# 根據(jù)兩類消息的ContentID來找出所有content# 這里先假設(shè)所有記錄為文字類型,圖片類型未知結(jié)果,待測試# Content_Text ctArray = []# Content_Image ciArray = []for msg in cur_to_t_msg:if msg.Type == 'text':ctArray.append(Content_Text.objects.filter(Cid = msg.ContentID))elif msg.Type == 'image':ciArray.append(Content_Image.objects.filter(Cid = msg.ContentID))for msg in t_to_cur_msg:if msg.Type == 'text':ctArray.append(Content_Text.objects.filter(Cid = msg.ContentID))elif msg.Type == 'image':ciArray.append(Content_Image.objects.filter(Cid = msg.ContentID))# 根據(jù)ContentID來進(jìn)行append,保證時間有序if len(Content_Text) >= 1:# 序列化,返回多條文字內(nèi)容serialized_obj = serializers.serialize('json', ctArray)response = {'state':'ok', 'msg':'ok', "data":serialized_obj}else:response['msg'] = 'no data'return HttpResponse(json.dumps(response), content_type = 'application/json')

具體邏輯

  • 定義返回體的內(nèi)容,包括一個state和msg
  • 判斷是否已經(jīng)登陸狀態(tài)
    • 是,直接返回HttpResponse,告訴前端已經(jīng)登陸,無須注冊
    • 否,下一步
  • 判斷方法是否為GET
  • 判斷查詢的用戶名是否為空
  • 獲取用戶登陸的用戶名以及密碼
  • 數(shù)據(jù)庫操作
    • 通過用戶名獲取當(dāng)前用戶發(fā)送給對方的信息
    • 通過用戶名獲取對方發(fā)送給當(dāng)前用戶的信息
    • 根據(jù)兩類消息的ContentID來找出所有content
    • 根據(jù)ContentID來進(jìn)行append,保證時間有序
    • 序列化,返回多條文字內(nèi)容

個人詳情頁面

效果截圖:

個人詳情頁面是使用UITableView來模仿微信聊天工具的詳情所制作的

InfoViewController.h

#import <UIKit/UIKit.h> #import "UserModel.h"NS_ASSUME_NONNULL_BEGIN@interface InfoViewController : UIViewController @property (weak, nonatomic) IBOutlet UILabel *Birthplace; @property (weak, nonatomic) IBOutlet UILabel *NickName; @property (weak, nonatomic) IBOutlet UILabel *ID; @property (weak, nonatomic) IBOutlet UILabel *Gender; @property (strong, nonatomic) UserModel* User; @property (weak, nonatomic) IBOutlet UIImageView *ProfilePicture;@end

首先在.h文件定義所用到的一些屬性,包括用戶名,地區(qū),性別,id號,頭像等,還有一個登陸后的User的信息。

InfoViewController.m

這里首先定義所用到TableView,以及左側(cè)的標(biāo)題列表,右側(cè)的內(nèi)容列表來存儲數(shù)據(jù)

#import "InfoViewController.h"@interface InfoViewController ()<UITableViewDelegate, UITableViewDataSource>@property(nonatomic, strong) UITableView *tableView; @property(nonatomic, strong) NSMutableArray<NSString*> *titleList; @property(nonatomic, strong) NSMutableArray<NSString*> *contentList;@end

viewDidLoad函數(shù)中加載數(shù)據(jù)

  • 綁定User中的信息到之前定義的屬性當(dāng)中。
  • 定義navigationItem的標(biāo)題名。
  • 獲取屏幕的寬與高來定義tableview視圖的大小與位置
  • 取消tableview默認(rèn)的多余的橫線
- (void)viewDidLoad {[super viewDidLoad];// [self.navigationController setNavigationBarHidden:NO animated:NO];self.navigationController.navigationBarHidden = NO;// Do any additional setup after loading the view.if (self.User == nil){self.User = [[UserModel alloc] initWithProperties:@"peppa ID" NickName:@"Peppa" RemarkName:@"peppy" Gender:@"female" Birthplace:@"UK" ProfilePicture:@"peppa.jpg"];}self.ProfilePicture.image = [UIImage imageNamed:self.User.ProfilePicture];self.NickName.text = self.User.NickName;self.ID.text = self.User.UserID;self.Gender.text = self.User.Gender;self.Birthplace.text = self.User.Birthplace;self.navigationItem.title = @"個人信息";// 獲取屏幕的寬高CGRect rect = [[UIScreen mainScreen] bounds];CGSize size = rect.size;CGFloat width = size.width;CGFloat height = size.height;self.tableView = ({UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, width, height/2+70) style:UITableViewStylePlain];tableView.delegate = self;tableView.dataSource = self;tableView;});// 取消多余的橫線self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];[self.view addSubview:self.tableView];[self loadData]; }

加載數(shù)據(jù),定義titleList,contentList

- (void)loadData {self.titleList = [NSMutableArray array];self.contentList = [NSMutableArray array];[self.titleList addObjectsFromArray:[[NSArray alloc] initWithObjects:@"頭像", @"昵稱", @"賬號", @"性別", @"地區(qū)",nil]];[self.contentList addObjectsFromArray:[[NSArray alloc] initWithObjects:@"小豬佩奇", @"Peppa", @"peppy", @"female", @"UK",nil]]; }
  • 用numberOfSectionsInTableView定義tableview的section數(shù)目
  • 用heightForRowAtIndexPath定義tableview每一個cell的高度
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1; }- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.titleList.count; }- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {return 80; }

根據(jù)不同的行來給每個cell自定義accessoryView, 并綁定不同的數(shù)據(jù)。這里我們第一行是圖片,故需要特殊處理來顯示圖片,而其他行則是顯示內(nèi)容,我這里修改一下它的字體與大小使得更加美觀,對比度更加高。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {NSString *cellID = [NSString stringWithFormat:@"cellID:%zd", indexPath.section];UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];if (nil == cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];}// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;cell.textLabel.text = self.titleList[indexPath.row];cell.textLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:18.f];if (indexPath.row != 0){UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,70,55)];rightLabel.text = self.contentList[indexPath.row];rightLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14.f];rightLabel.textColor = [UIColor grayColor];cell.accessoryView = rightLabel;//cell.accessoryView.backgroundColor = [UIColor redColor]; //加上紅色容易看清楚}else{cell.accessoryView = ({UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.User.ProfilePicture]];CGRect frame = imgV.frame;frame = CGRectMake(0, 0, 100, 55);imgV.frame = frame;[imgV setContentMode:UIViewContentModeScaleAspectFit];imgV;});}return cell; }

點擊列表的item時跳轉(zhuǎn)至修改頁面

#pragma mark ------------ UITableViewDelegate ------------------- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[tableView deselectRowAtIndexPath:indexPath animated:YES];InfoViewController *controller = [[InfoViewController alloc] init];controller.hidesBottomBarWhenPushed = YES;[self.navigationController pushViewController:controller animated:YES]; }

用戶退出按鈕綁定事件,這里要與服務(wù)器進(jìn)行交互,刪除之前登陸的session

- (IBAction)logout:(id)sender {NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:nil delegateQueue:[NSOperationQueue mainQueue]];NSURL *url = [NSURL URLWithString:@"http://118.89.65.154:8000/account/logout/"];NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {if(error == nil) {if(NSClassFromString(@"NSJSONSerialization")) {NSError *e = nil;id object = [NSJSONSerialization JSONObjectWithData:data options:0 error:&e];if(e) {NSLog(@"error");}if([object isKindOfClass:[NSDictionary class]]) {NSDictionary *result = object;if([result[@"state"] isEqualToString:@"ok"]) {NSLog(@"logout success");UIStoryboard *indexStoryboard = [UIStoryboard storyboardWithName:@"Index" bundle:nil];[UIApplication sharedApplication].keyWindow.rootViewController = indexStoryboard.instantiateInitialViewController;}else {NSLog(@"logout fail");}}else {NSLog(@"Not dictionary");}}}else {NSLog(@"網(wǎng)絡(luò)異常");}}];[task resume]; }

總結(jié)

以上是生活随笔為你收集整理的IOS开发笔记(九)——IM聊天工具个人详情页面,自定义tableview的accessaryView的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 老司机午夜福利视频 | 国产精品视频网 | 日韩欧美第一页 | 美梦视频大全在线观看高清 | 亚洲精品视频久久久 | 日本一区二区三区网站 | 国产影视一区 | 亚洲第一成肉网 | 毛片福利视频 | 色网址在线观看 | 岛国av电影在线观看 | 久草一本 | 91午夜在线| 人妻丰满熟妇av无码久久洗澡 | beeg日本高清xxxx18| 青青视频在线播放 | 成年人视频网址 | 捆绑无遮挡打光屁股 | 亚洲激情视频在线播放 | 伊人精品一区二区三区 | av在线黄 | yjizz视频网 国产乱人对白 | 成人h片在线观看 | 亚洲人xxx日本人18 | 深夜福利一区 | 91精品国产91久久久久 | 九九热精品视频在线观看 | 欧美国产综合 | 日韩视频中文字幕 | www成人在线观看 | 在线免费黄网 | 日本一区二区三区在线观看 | 极品国产91在线网站 | www.狠狠操.com | 五月天丁香婷 | 99这里精品| 日日操网站 | 日韩伦乱| www夜夜 | 在线观看高清av | 日韩黄色精品视频 | 91传媒视频在线观看 | 四虎精品视频 | 欧美黄色xxx | 最新中文字幕在线 | 中文字幕av在线免费观看 | 天天干天天干 | 三级视频网站 | 在线看片你懂得 | 欧美日韩综合一区二区 | 日本一区二区三区在线免费观看 | 亚洲熟女乱色一区二区三区久久久 | 久久精品免费看 | 欧美黄片一区二区三区 | 中文字幕狠狠 | 一本色道综合久久欧美日韩精品 | 国产精品搬运 | 热久久在线 | 欧美福利视频导航 | 久操精品 | 国产欧美一区二区三区另类精品 | 国产成人综合在线 | 人妻熟女一区二区三区 | 超碰在线99 | 白峰美羽在线播放 | 朝桐光av在线一区二区三区 | 欧美视频亚洲 | 男男做的视频 | 欧美日韩麻豆 | 国产在线自 | 日本一级二级视频 | 午夜精品久久久久久久久久久久 | 久草资源在线 | 欧美影院一区二区三区 | 欧美少妇一区 | 中文字幕四区 | 欧美视频亚洲 | 97福利网| 日本三级小视频 | 国产欧美一区二区精品久久久 | 91日韩 | 91亚洲精品在线观看 | 精品国产一区二区三 | 激情 小说 亚洲 图片 伦 | 国产理论av | 日韩综合一区二区三区 | 中文字幕成人在线 | 91精品亚洲一区 | 日本免费不卡 | 欧美jizzhd欧美18 | 精品久久久av | 国产αv| 国产福利视频网站 | 无码人妻aⅴ一区二区三区玉蒲团 | 国产精品99久久久久 | 成人一区二区电影 | 美女扣逼喷水视频 | 一个人看的www视频在线观看 | 91sex国产|