如何在一个UITableView中创建多个列表?IOS斯威夫特
谁能帮帮我吗。我将UITableView分为两个部分,如何在一个UITableView中创建多个列表?IOS斯威夫特,ios,objective-c,uitableview,swift,Ios,Objective C,Uitableview,Swift,谁能帮帮我吗。我将UITableView分为两个部分,已注册和未注册这是我的视图 但是我不知道如何将我的json数据绘制成这个。这是我的数据 [ { “项目名称”:“玻璃101”, “地位”:1 }, { “项目名称”:“玻璃102”, “状态”:0 }, { “项目名称”:“玻璃103”, “状态”:0 }, { “项目名称”:“玻璃104”, “地位”:1 }, { “项目名称”:“玻璃105”, “状态”:0 }, { “项目名称”:“玻璃106”, “地位”:1 } ] 如果状态等于0
已注册
和未注册
这是我的视图
但是我不知道如何将我的json数据绘制成这个。这是我的数据
[
{
“项目名称”:“玻璃101”,
“地位”:1
},
{
“项目名称”:“玻璃102”,
“状态”:0
},
{
“项目名称”:“玻璃103”,
“状态”:0
},
{
“项目名称”:“玻璃104”,
“地位”:1
},
{
“项目名称”:“玻璃105”,
“状态”:0
},
{
“项目名称”:“玻璃106”,
“地位”:1
}
]
如果状态等于0
我将其放入未注册
,那么如果1
我将其放入已注册
,我需要做什么
我现在有一个简单的列表,一个注册和未注册的列表。我想知道我怎样才能做到这一点
任何评论、建议或链接都会大有帮助,因为我不知道如何开始。提前感谢
让我们考虑您的数据存储在一个名为TEMPARRY的数组中。然后这样做:
NSMutableArray *registered = [[NSMutableArray alloc] init];
NSMutableArray *notRegistered = [[NSMutableArray alloc] init];
for (int i = 0 ; i < tempArray.count ; i++) {
if ([[[tempArray objectAtIndex:i] objectForKey:@"status"] boolValue]) {
[registered addObject:[tempArray objectAtIndex:i]];
}
else{
[notRegistered addObject:[tempArray objectAtIndex:i]];
}
}
然后在cellforrowatinexpath
中执行以下操作:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 1){
return registered.count;
}
return notRegistered.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
if (indexpath.section == 0) {
cell.textLabel.text = [[notRegistered objectAtIndex:indexpath.row] objectForKey:@"itemName"];
}
else {
cell.textLabel.text = [[registered objectAtIndex:indexpath.row] objectForKey:@"itemName"];
}
return cell;
}
让我们考虑您的数据存储在一个名为TEMPARRY的数组中。然后这样做:
NSMutableArray *registered = [[NSMutableArray alloc] init];
NSMutableArray *notRegistered = [[NSMutableArray alloc] init];
for (int i = 0 ; i < tempArray.count ; i++) {
if ([[[tempArray objectAtIndex:i] objectForKey:@"status"] boolValue]) {
[registered addObject:[tempArray objectAtIndex:i]];
}
else{
[notRegistered addObject:[tempArray objectAtIndex:i]];
}
}
然后在cellforrowatinexpath
中执行以下操作:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 1){
return registered.count;
}
return notRegistered.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
if (indexpath.section == 0) {
cell.textLabel.text = [[notRegistered objectAtIndex:indexpath.row] objectForKey:@"itemName"];
}
else {
cell.textLabel.text = [[registered objectAtIndex:indexpath.row] objectForKey:@"itemName"];
}
return cell;
}
您可以解析json并根据您的条件创建两个数组,
status=0
,将当前索引的整个对象放入未注册的数组中
,如果status==1
,则将当前索引的整个对象放入已注册的数组中
NSMutableArray *registered_Array = [[NSMutableArray alloc] init]; //Define this, out of any method, may be above `viewDidLoad`
NSMutableArray *not_registered_Array = [[NSMutableArray alloc] init];
for (int i = 0 ; i < yourRespomseArray.count ; i++) {
if ([[[yourRespomseArray objectAtIndex:i] objectForKey:@"status"] boolValue]) {
[registered_Array addObject:[yourRespomseArray objectAtIndex:i]];
}
else{
[not_registered_Array addObject:[yourRespomseArray objectAtIndex:i]];
}
}
享受编码的乐趣 您可以解析json并根据您的条件创建两个数组,
status=0
,将当前索引的整个对象放入not_registered_数组
,如果status==1
,则将当前索引的整个对象放入registered_数组
NSMutableArray *registered_Array = [[NSMutableArray alloc] init]; //Define this, out of any method, may be above `viewDidLoad`
NSMutableArray *not_registered_Array = [[NSMutableArray alloc] init];
for (int i = 0 ; i < yourRespomseArray.count ; i++) {
if ([[[yourRespomseArray objectAtIndex:i] objectForKey:@"status"] boolValue]) {
[registered_Array addObject:[yourRespomseArray objectAtIndex:i]];
}
else{
[not_registered_Array addObject:[yourRespomseArray objectAtIndex:i]];
}
}
享受编码的乐趣 实现此数据源方法。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return [statusZero count]
}
else{
return [statusOne count]
}
}
使用FilteredarrayingPredicate
获取两个不同的数组对象,一个用于状态0,另一个用于状态1
然后实施此方法。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return [statusZero count]
}
else{
return [statusOne count]
}
}
最后使用此方法添加单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0){
// add status 1 array cell
}
else{
// add status 0 array cell
}
return cell
}
实现此数据源方法。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return [statusZero count]
}
else{
return [statusOne count]
}
}
使用FilteredarrayingPredicate
获取两个不同的数组对象,一个用于状态0,另一个用于状态1
然后实施此方法。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
if(section == 0){
return [statusZero count]
}
else{
return [statusOne count]
}
}
最后使用此方法添加单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if(indexPath.section == 0){
// add status 1 array cell
}
else{
// add status 0 array cell
}
return cell
}
我不确定您是否无法将JSON解析为对象,或者不知道如何将
UITableView
拆分为多个部分
根据对象的使用情况,您需要解析JSON并使用字典中的值实例化对象(数组中的每个记录都可以放在字典中)。例如,请参见以下内容:
我并不是说这是将JSON解析为对象的最佳方式,而是让您开始的一种方式
使用filteredArrayUsingPredicate
将数组筛选为属性上的两个数组。比如说:
let myCompleteArray = {};
var registered = myCompleteArray.filter ({ $0.status == true });
var unregistered = myCompleteArray.filter( { $0.status == false });
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return registered.count;
}
else {
return unregistered.count;
}
}
然后需要实现UITableViewDataSource
,如下所示:
let myCompleteArray = {};
var registered = myCompleteArray.filter ({ $0.status == true });
var unregistered = myCompleteArray.filter( { $0.status == false });
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return registered.count;
}
else {
return unregistered.count;
}
}
当然,您需要更改cellForRowAtIndexPath
以检查正在呈现哪个部分(就像上面的numberOfRowsInSection
中一样)。我不确定您是否无法将JSON解析为对象,或者您不知道如何将UITableView
拆分为多个部分
根据对象的使用情况,您需要解析JSON并使用字典中的值实例化对象(数组中的每个记录都可以简单地放入字典中)。例如,请参见以下内容:
我并不是说这是将JSON解析为对象的最佳方式,而是让您开始的一种方式
使用filteredArrayUsingPredicate
,将数组筛选为属性上的两个数组
let myCompleteArray = {};
var registered = myCompleteArray.filter ({ $0.status == true });
var unregistered = myCompleteArray.filter( { $0.status == false });
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return registered.count;
}
else {
return unregistered.count;
}
}
然后需要实现UITableViewDataSource
,如下所示:
let myCompleteArray = {};
var registered = myCompleteArray.filter ({ $0.status == true });
var unregistered = myCompleteArray.filter( { $0.status == false });
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 2;
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section == 0) {
return registered.count;
}
else {
return unregistered.count;
}
}
当然,您需要更改单元格的行索引路径
以检查正在呈现哪个部分(就像上面的numberofrowsinssection
中一样。您只有两个状态正确(0或1)?将JSON过滤成两个数组-查看filteredArrayUsingPredicate
,然后对每个表节使用适当的数组是sir 0表示未注册,1表示已注册@AkashShinde@Paulw11我可以对数组进行过滤,但我不知道如何在每个表节中显示它,只需在cellForRowAtIndexPath
和NumberOfRowInSection
您只有两个状态权限(0或1)?将JSON过滤成两个数组-查看filteredArrayUsingPredicate
,然后对每个表节使用适当的数组是sir 0表示未注册,1表示已注册@AkashShinde@Paulw11我可以对数组进行过滤,但我不知道如何在每个表节中显示它,只需在cellForRowAtIndexPath
和numberOfRowsInSection
这是一个非常漂亮的代码,用于识别哪个是哪个,谢谢你,先生。但是现在缺少的是cellForRowAtIndexPath中的代码,我不知道如何显示我正在添加的名称。等等,这是一个非常漂亮的代码,用于识别哪个是哪个,谢谢先生。但是现在缺少的是CellForRowatineXpath中的代码,我不知道如何显示我要添加的名称。等等,这就是我缺少的!感谢您的大力帮助。-在swiftso中编写它也应该得到一个加号,这就是