Привет, я новичок в iOS и SO. Я столкнулся с проблемой в своем приложении, чтобы отобразить две настраиваемые ячейки в одном табличном представлении с выбором сегментированного элемента управления. Когда пользователь нажимает на сегментированный = 0, он отображает одну пользовательскую ячейку, а сегментированный = 1 отображает вторую пользовательскую ячейку. Вот мой код, который я пробовал до сих пор,
-(void)callSegmentSelected
{
value1 = (int)segment.selectedSegmentIndex;
if (segment.selectedSegmentIndex == 0)
{
NSString *urlString = [NSString stringWithFormat:@"http:"url"];
NSString *jsonString = [NSString stringWithFormat:catid];
NSData *myJSONData =[jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
[body appendData:[NSData dataWithData:myJSONData]];
[request setHTTPBody:body];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
if(str.length > 0)
{
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
array =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
[myTableView reloadData];
}
else
{
NSLog(@"Error");
}
}
else if (segment.selectedSegmentIndex == 1)
{
}
Вот мои методы делегата tableview
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (value1 == 0)
{
static NSString *cellId = @"Cell";
Cell1 * cell = (Cell1 *)[myTableView dequeueReusableCellWithIdentifier:cellId];
if (cell == nil)
{
NSArray *myNib;
myNib = [[NSBundle mainBundle]loadNibNamed:@"Cell1" owner:self options:nil];
cell = [myNib lastObject];
}
cell.examLbl.text = [[array objectAtIndex:indexPath.row]objectForKey:@"examtitle"];
myTableView.dataSource = self;
myTableView.delegate = self;
[myTableView reloadData];
return cell;
}
else
{
if (value1 == 1)
{
static NSString * cellIdentifier = @"Cell2";
Cell2 *cell = (ContestRewardCell *)[myTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *myNib;
myNib = [[NSBundle mainBundle]loadNibNamed:@"cell2" owner:self options:nil];
cell =[myNib objectAtIndex:0];
}
cell.titleLbl.text = [[array objectAtIndex:indexPath.row]objectForKey:@"title"];
myTableView.dataSource = self;
myTableView.delegate = self;
[myTableView reloadData];
return cell;
}
}
return nil;
}
В этом коде нет ошибок, я проверил в режиме отладки, что данные будут поступать в «cell.examLbl.text», но не отображать пользовательскую ячейку в представлении таблицы. В чем я ошибся в этом коде. Пожалуйста, помогите мне. Заранее спасибо.
Вот ответ на мою проблему, я не реализовал это
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (value1 == 0)
{
return 102;
}
else if(value1 == 1)
{
return 193;
}
return 0;
}