1.做好准备:引入包,声明等。
<span style="font-size:24px;">#import "EquipTypeListViewController.h"
#import "EquipNetworking.h"//网络获取
#import "GeneralConfig.h"//通用配置
#import "UIDevice-Hardware.h"//检测设备硬件
#import "EquipListViewController.h"</span><pre name="code" class="objc"><span style="font-size:24px;">static NSString *const cellIdentify = @"EquipType";
@interface EquipTypeListViewController ()
@property (nonatomic, strong) NSArray *equipArray;
@end</span>
2.把网络获取需要的资源保存到当前类,再初始化TableView
[EquipNetworking getEquipTypeList:^(NSArray *equipList, NSError *error) {
self.equipArray = equipList;
[self initEquipTypeListTableView];
}];
- (void)initEquipTypeListTableView{
UITableView *equipTypeListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT-64)];
//判断iOS7.0之后tableview的分割线偏移
if ([[[UIDevice currentDevice] systemVersion] intValue] > 7.0) {
equipTypeListTable.separatorInset = UIEdgeInsetsZero;
equipTypeListTable.layoutMargins = UIEdgeInsetsZero;
}
equipTypeListTable.delegate = self;
equipTypeListTable.dataSource = self;
[self.view addSubview:equipTypeListTable];
}
设置分割线需要在TableView里设置
equipTypeListTable.separatorInset = UIEdgeInsetsZero;
equipTypeListTable.layoutMargins = UIEdgeInsetsZero;
在TableViewCell里设置
cell.layoutMargins = UIEdgeInsetsZero;
#pragma mark - UITableView的相关代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.equipArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
}
//判断iOS7.0之后tableview的分割线偏移
if ([[[UIDevice currentDevice] systemVersion] intValue] > 7.0) {
cell.layoutMargins = UIEdgeInsetsZero;
}
cell.textLabel.text = [[self.equipArray objectAtIndex:indexPath.row] valueForKey:@"text"];
return cell;
}
5.需要点击进入后实现功能的话就调用代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{//取消选中某一行(去掉cell选中时默认的蓝色背景)[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}
下面是全部的代码:
//
// EquipTypeListViewController.m
// LoLBox_New
//
// Created by ceshi on 14/12/22.
// Copyright (c) 2014年 ceshi. All rights reserved.
//
#import "EquipTypeListViewController.h"
#import "EquipNetworking.h"
#import "GeneralConfig.h"
#import "UIDevice-Hardware.h"
#import "EquipListViewController.h"
static NSString *const cellIdentify = @"EquipType";
@interface EquipTypeListViewController ()
@property (nonatomic, strong) NSArray *equipArray;
@end
@implementation EquipTypeListViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self configTitleAndBackItem:nil];
self.title = @"装备分类";
[EquipNetworking getEquipTypeList:^(NSArray *equipList, NSError *error) {
self.equipArray = equipList;
[self initEquipTypeListTableView];
}];
// Do any additional setup after loading the view.
}
- (void)initEquipTypeListTableView{
UITableView *equipTypeListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, P_WIDTH, P_HEIGHT-64)];
//判断iOS7.0之后tableview的分割线偏移
if ([[[UIDevice currentDevice] systemVersion] intValue] > 7.0) {
equipTypeListTable.separatorInset = UIEdgeInsetsZero;
equipTypeListTable.layoutMargins = UIEdgeInsetsZero;
}
equipTypeListTable.delegate = self;
equipTypeListTable.dataSource = self;
[self.view addSubview:equipTypeListTable];
}
#pragma mark - UITableView的相关代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.equipArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
}
//判断iOS7.0之后tableview的分割线偏移
if ([[[UIDevice currentDevice] systemVersion] intValue] > 7.0) {
// cell.separatorInset = UIEdgeInsetsZero;
cell.layoutMargins = UIEdgeInsetsZero;
}
cell.textLabel.text = [[self.equipArray objectAtIndex:indexPath.row] valueForKey:@"text"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
EquipListViewController *equipList = [[EquipListViewController alloc] initWithTag:[[self.equipArray objectAtIndex:indexPath.row] valueForKey:@"tag"] title:[[self.equipArray objectAtIndex:indexPath.row] valueForKey:@"text"]];
[self.navigationController pushViewController:equipList animated:YES];
}
- (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