我有一个数组(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
答案 0 :(得分:0)
#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];
}