Files
midi_ios/QXLive/Dynamic(语圈)/View/QXTopicListView.m
2025-09-19 11:38:43 +08:00

203 lines
7.2 KiB
Objective-C

//
// QXTopicListView.m
// QXLive
//
// Created by 启星 on 2025/5/28.
//
#import "QXTopicListView.h"
@interface QXTopicListView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *selectedArray;
@property (nonatomic,strong)UILabel* titleLabel;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)UIButton* cancelBtn;
@property (nonatomic,strong)UIButton* commitBtn;
@end
@implementation QXTopicListView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubviews];
}
return self;
}
-(void)initSubviews{
self.page = 1;
self.backgroundColor = [UIColor whiteColor];
[self addRoundedCornersWithRadius:16 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-100)/2, 16, 100, 27)];
self.titleLabel.text = QXText(@"选择话题");
self.titleLabel.textColor = QXConfig.textColor;
self.titleLabel.font = [UIFont boldSystemFontOfSize:18];
[self addSubview:self.titleLabel];
self.cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 12, 65, 35)];
[self.cancelBtn setTitle:QXText(@"取消") forState:(UIControlStateNormal)];
[self.cancelBtn setTitleColor:RGB16(0x333333) forState:(UIControlStateNormal)];
self.cancelBtn.titleLabel.font = [UIFont systemFontOfSize:14];
[self.cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:(UIControlEventTouchUpInside)];
[self addSubview:self.cancelBtn];
self.commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-65, 12, 65, 35)];
[self.commitBtn setTitle:QXText(@"确定") forState:(UIControlStateNormal)];
[self.commitBtn setTitleColor:QXConfig.themeColor forState:(UIControlStateNormal)];
self.commitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
// self.commitBtn.backgroundColor = QXConfig.themeColor;
[self.commitBtn addRoundedCornersWithRadius:17.5];
[self.commitBtn addTarget:self action:@selector(commitAction) forControlEvents:(UIControlEventTouchUpInside)];
[self addSubview:self.commitBtn];
[self addSubview:self.tableView];
[self getTopic];
}
-(void)getTopic{
MJWeakSelf
[weakSelf.selectedArray removeAllObjects];
[QXDynamicNetwork getTopicListWithPage:self.page isTopTopic:NO
successBlock:^(NSArray<QXTopicModel *> * _Nonnull hotos) {
if (weakSelf.page == 1) {
[weakSelf.dataArray removeAllObjects];
}
[weakSelf.dataArray addObjectsFromArray:hotos];
[weakSelf.tableView.mj_header endRefreshing];
if (hotos.count == 0) {
weakSelf.tableView.mj_footer.state = MJRefreshStateNoMoreData;
}else{
[weakSelf.tableView.mj_footer endRefreshing];
}
[self.tableView reloadData];
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
[weakSelf.tableView.mj_header endRefreshing];
[weakSelf.tableView.mj_footer endRefreshing];
}];
}
-(void)cancelAction{
[[QXGlobal shareGlobal] hideViewBlock:^{}];
}
-(void)commitAction{
MJWeakSelf
[[QXGlobal shareGlobal] hideViewBlock:^{
if (weakSelf.selecctedTopicBlock) {
weakSelf.selecctedTopicBlock(weakSelf.selectedArray);
}
}];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QXTopicListViewCell *cell = [QXTopicListViewCell cellWithTableView:tableView];
QXTopicModel *model = self.dataArray[indexPath.row];
cell.model = model;
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
QXTopicModel *model = self.dataArray[indexPath.row];
model.isSelected = !model.isSelected;
[tableView reloadRow:indexPath.row inSection:indexPath.section withRowAnimation:(UITableViewRowAnimationAutomatic)];
if (model.isSelected) {
[self.selectedArray addObject:model];
}else{
[self.selectedArray removeObject:model];
}
}
-(UITableView *)tableView{
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom+8, self.width, self.height-16-self.titleLabel.bottom-8) style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor clearColor];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.rowHeight = 48;
MJWeakSelf
_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
weakSelf.page = 1;
[weakSelf getTopic];
}];
_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
weakSelf.page++;
[weakSelf getTopic];
}];
}
return _tableView;
}
-(NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
-(NSMutableArray *)selectedArray{
if (!_selectedArray) {
_selectedArray = [NSMutableArray array];
}
return _selectedArray;
}
@end
@implementation QXTopicListViewCell
+(instancetype)cellWithTableView:(UITableView *)tableView{
static NSString* cellId = @"QXTopicListViewCell";
QXTopicListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (!cell) {
cell = [[QXTopicListViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellId];
}
return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initSubviews];
}
return self;
}
-(void)initSubviews{
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.textColor = QXConfig.textColor;
self.titleLabel.font = [UIFont systemFontOfSize:14];
[self.contentView addSubview:self.titleLabel];
self.rightBtn = [[UIButton alloc] init];
[self.rightBtn setImage:[UIImage imageNamed:@"login_agreement_sel"] forState:(UIControlStateSelected)];
[self.rightBtn setImage:[UIImage imageNamed:@"login_agreement_nor"] forState:(UIControlStateNormal)];
self.rightBtn.userInteractionEnabled = NO;
[self.contentView addSubview:self.rightBtn];
[self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-16);
make.height.width.mas_equalTo(20);
make.centerY.equalTo(self.contentView);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(16);
make.height.mas_equalTo(24);
make.centerY.equalTo(self.contentView);
make.right.equalTo(self.rightBtn.mas_left).offset(-10);
}];
}
-(void)setModel:(QXTopicModel *)model{
_model = model;
self.titleLabel.text = model.title;
self.rightBtn.selected = model.isSelected;
}
@end