ios 数据库 oc uitableView 展示单组数据 - // 背景颜色

📅 2026-12-01 👤 拼多多助力营销技巧 📂 首码项目 👁️ 141 次阅读

设置模型

#import

@interface XMGWine : NSObject

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *image;

@property (nonatomic,copy) NSString *money;

+(instancetype)wineWithDict:(NSDictionary *)dict;

@end

#import "XMGWine.h"

@implementation XMGWine

+(instancetype)wineWithDict:(NSDictionary *)dict{

XMGWine *wine = [[self alloc]init];

[wine setValuesForKeysWithDictionary:dict];

return wine;

}

@end

//

// ViewController.m

// OcDemoTest

//

// Created by Mac on 2023/7/14.

//

#import "ViewController.h"

#import "XMGWine.h"

// 添加协议

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;

// 所有饿酒数据

@property (nonatomic,strong) NSArray *wineArray;

@end

@implementation ViewController

// 加载数据

-(NSArray *)wineArray{

if(!_wineArray){

// 加载plist文件

NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"wine.plist" ofType:nil]];

// 转化成模型

NSMutableArray *temp = [NSMutableArray array];

for (NSDictionary *wineDict in dictArray) {

[temp addObject:[XMGWine wineWithDict:wineDict]];

}

_wineArray = temp;

}

return _wineArray;

}

- (void)viewDidLoad {

[super viewDidLoad];

// 设置数据源

self.tableView.dataSource = self;

}

// 一共有多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

return 1;

}

// 每一组都多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.wineArray.count;

}

// 每行显示的内容

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

// 设置样式

UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

// UITableViewCell *cell = [[UITableViewCell alloc]init];

// 取出indexpath 对应的酒模型

// 箭头

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

XMGWine *wine = self.wineArray[indexPath.row];

// 设置数据

cell.textLabel.text = wine.name;

cell.imageView.image = [UIImage imageNamed:wine.image];

cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%@",wine.money];

// 设置文字颜色

cell.detailTextLabel.textColor = [UIColor orangeColor];

return cell;

}

@end

self.tableView.dataSource = self;

// 常见属性

// 设置行高

self.tableView.rowHeight = 100;

// 头部高度

self.tableView.sectionHeaderHeight = 100;

// 尾部高度

self.tableView.sectionFooterHeight = 100;

// 分割线的颜色

self.tableView.separatorColor = [UIColor redColor];

//self.tableView.separatorColor = [UIColor clearColor]; // 没有颜色

// 设置分割线的样式

// self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

// 设置头部尾部的控件

// self.tableView.tableHeaderView = [[UISwitch alloc]init];

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

设置cell

// 设置样式

UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

// UITableViewCell *cell = [[UITableViewCell alloc]init];

// 取出indexpath 对应的酒模型

// 设置右边显示的指示样式

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

// 设置右边显示的控件

cell.accessoryView = [[UISwitch alloc]init];

// 选中的颜色 没有效果

// cell.selectionStyle = UITableViewCellSelectionStyleBlue;

// 背景颜色

// cell.backgroundColor = [UIColor redColor];

// 设置cell选中的背景颜色

UIView *selectedBg = [[UIView alloc]init];

selectedBg.backgroundColor = [UIColor grayColor];

cell.selectedBackgroundView = selectedBg;

XMGWine *wine = self.wineArray[indexPath.row];

// 设置数据

cell.textLabel.text = wine.name;

cell.imageView.image = [UIImage imageNamed:wine.image];

cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%@",wine.money];

// 设置文字颜色 拼多多帮助力入口

cell.detailTextLabel.textColor = [UIColor orangeColor];

代理方法

@interface ViewController ()

// 设置代理

self.tableView.delegate = self;

// 代理方法 选中某一行cell就会调用这个方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"选中了:%ld",indexPath.row);

}

// 取消选中的某一行

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

NSLog(@"取消了:%ld",indexPath.row);

}

// 设置头部控件

//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

//

//}

UITableViewController

精彩内容

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧