UITableViewCell 存储在其他数组中
Posted
技术标签:
【中文标题】UITableViewCell 存储在其他数组中【英文标题】:UITableViewCell stored in other array 【发布时间】:2016-06-30 07:32:53 【问题描述】:我有一个数组(arrData)和 UITableViewCell 重新加载 arrData。 arrData 存储在 Web 服务对象数组中。 UITableViewCell 中一个二标签一图片,标签和图片通过arrData 加载数据。所以所有标签和图像都重新加载在同一个数组中,但我想为每个标签和图像重新加载不同的数组。
视图控制器
#import "ViewController.h"
#import "MemberTableViewCell.h"
#import "member_details.h"
@interface ViewController ()
NSArray *arrData;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
[self.tabel_view setAllowsMultipleSelection:YES];
NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_member.php"]];
response =[[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:req delegate:self];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[response appendData:data];
NSLog(@"error receving data %@",response);
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
NSError *error;
NSLog(@"Error in receiving data %@",error);
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"response data %@",json);
NSArray *status = json[@"status"];
arrData = status;
[self.tabel_view reloadData];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
NSArray<NSIndexPath *> *selectedRows = [tableView indexPathsForSelectedRows];
if (selectedRows && [selectedRows containsObject:indexPath])
return 127.0; // Expanded height
return 50.0; // Normal height
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [arrData count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
MemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ht"];
if (cell==nil)
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.name.text= [[arrData objectAtIndex:indexPath.row] valueForKey:@"business_category_name"];
cell.title.text= [[[[arrData objectAtIndex:indexPath.row] valueForKey:@"business_details"] objectAtIndex:0] valueForKey:@"name"];
cell.email.text=[[[[arrData objectAtIndex:indexPath.row] valueForKey:@"business_details"] objectAtIndex:0] valueForKey:@"email"];
cell.image_view.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[[[arrData objectAtIndex:indexPath.row]valueForKey:@"business_details"]objectAtIndex:0] valueForKey:@"img_url"]]]];
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[self updateTableView];
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
[self updateTableView];
- (void)updateTableView
[self.tabel_view beginUpdates];
[self.tabel_view endUpdates];
@end
【问题讨论】:
你的问题不清楚,提供arrData中包含的数据。同时让我们知道您是在使用 xib 的原型单元还是自定义单元? @BharatModi 带有情节提要的自定义单元格,我将编辑我的问题。 @BharatModi 我将更新我的问题,请再次检查。谢谢 那你为什么要从笔尖加载单元格呢?如果您使用的是原型单元格,那么您的 cellForRowAtIndexPath 应该如下所示, cellIdentifier = @"cellReviews";单元格 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];您是否记录了 arrData,提供相同的内容。 【参考方案1】:试试这个代码
#import "ViewController.h"
@interface ViewController ()
IBOutlet UITableView *tbl;
NSMutableData *response;
NSMutableArray *Arrdata;
NSMutableArray *ArrySubData;
@end
@implementation ViewController
- (void)viewDidLoad
[super viewDidLoad];
Arrdata=[[NSMutableArray alloc]init];
ArrySubData=[[NSMutableArray alloc]init];
[tbl setAllowsMultipleSelection:YES];
NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_member.php"]];
response =[[NSMutableData alloc]init];
[NSURLConnection connectionWithRequest:req delegate:self];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
[response appendData:data];
NSLog(@"error receving data %@",response);
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
NSError *error;
NSLog(@"Error in receiving data %@",error);
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"response data %@",json);
NSArray *status = json[@"status"];
Arrdata = status;
NSLog(@"%@",Arrdata);
[tbl reloadData];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
ArrySubData=[[Arrdata objectAtIndex:section] objectForKey:@"business_details"];
return ArrySubData.count;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
return [NSString stringWithFormat:@" %@",[[Arrdata objectAtIndex:section] objectForKey:@"business_category_name"]];
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 25;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
UILabel *myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(0, 0, 320, 20);
myLabel.font = [UIFont fontWithName:@"Roboto-Bold" size:13.0];
myLabel.textColor=[UIColor whiteColor];
myLabel.backgroundColor=[UIColor colorWithRed:70.0/255.0 green:82.0/255.0 blue:88.0/255.0 alpha:1.0];
myLabel.text = [self tableView:tableView titleForHeaderInSection:section];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView *ImgBrands = (UIImageView *) [cell viewWithTag:101];
UILabel *lblName = (UILabel *) [cell viewWithTag:102];
UILabel *lblEmail = (UILabel *) [cell viewWithTag:103];
UILabel *lblPhone = (UILabel *) [cell viewWithTag:104];
ArrySubData=[[Arrdata objectAtIndex:indexPath.section] objectForKey:@"business_details"];
NSString *strName=[NSString stringWithFormat:@"%@",[[ArrySubData objectAtIndex:indexPath.row] objectForKey:@"name"]];
NSString *stremail=[NSString stringWithFormat:@"%@",[[ArrySubData objectAtIndex:indexPath.row] objectForKey:@"email"]];
NSString *strphone=[NSString stringWithFormat:@"%@",[[ArrySubData objectAtIndex:indexPath.row] objectForKey:@"phone"]];
NSString *strimage=[NSString stringWithFormat:@"%@",[[ArrySubData objectAtIndex:indexPath.row] objectForKey:@"image"]];
lblName.text=strName;
lblEmail.text=stremail;
lblPhone.text=strphone;
ImgBrands.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strimage]]];
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
[self updateTableView];
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
[self updateTableView];
- (void)updateTableView
[tbl beginUpdates];
[tbl endUpdates];
【讨论】:
您可以在部分显示business_category_name,在单元格中显示地址、手机、电子邮件和图片以上是关于UITableViewCell 存储在其他数组中的主要内容,如果未能解决你的问题,请参考以下文章
我想从数组中 UITableviewCell 的文本字段中添加字符串
如何将 JSON 数组中的所有记录加载到 UITableViewCell 中?
从 Firebase 填充数组传递 UITableViewCell 数据
UITableViewCell 中的 3D Touch Peek 和 Pop 如何将数据移交给其他 UIViewController?