Reputation: 59
I have to create an UITableView
that contains 2 custom cells RestTime
and ExerciseTime
. That after an ExerciseTime
is a RestTime
cell.
Here is the design of my tableView:
And here is my implement code:
-Cell's height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// RestTime
if (indexPath.row % 2 == 1) {
return 40.0f;
}
// ExerciseTime
else {
return 65.0f;
}
}
-Number of Cells
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (self.preset.blocks.count * 2) - 1;
}
-Cell for row
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row % 2 == 1) {
RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier forIndexPath:indexPath];
RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:indexPath.row];
//CustomCell
return restTimeCell;
}else{
ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier forIndexPath:indexPath];
//Cell index
int index = (int)(indexPath.row / 2);
//exerciseTimes is a NSSet
ExerciseTime *exerciseTime = [self.preset.exerciseTimes.allObjects objectAtIndex:index];
//CustomCell
return exerciseTimecell;
}
return nil;
}
And the output is this :
I have tried to covert my NSSet to NSMutableArray and its still now working.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row % 2 == 1) {
RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:RestTimeTableViewCellIdentifier forIndexPath:indexPath];
RestTime *restTime = (RestTime *)[self.restTimeArray objectAtIndex:indexPath.row];
//CustomCell
return restTimeCell;
}else{
ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:ExerciseTimeTableViewCellIdentifier forIndexPath:indexPath];
NSMutableArray *array = [[self.preset.exerciseTimes allObjects] mutableCopy];
int index = (int)(indexPath.row / 2);
ExerciseTime *exerciseTime = [array objectAtIndex:index];
//CustomCell
return exerciseTimecell;
}
return nil;
}
As you can see that all the ExerciseCell
is not in the right order. I cant figure out why it isn't in the right index after the cell is created. I want the order is sorted by the time its created not alphabet abcdef... or 123456... . Can anyone help me to find out what is the problem and how to tackle it.
Upvotes: 0
Views: 719
Reputation: 59
I had found the problem and it was the NSSet is not sorted at the beginning and I sorted it with @"createdTime" and my problem was solved.
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"createdTime" ascending:YES];
NSArray *sortedArray = [self.exerciseTimes sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
Exercise *exerciseTime = (Exercise *)[sortedArray objectAtIndex:indexPath.row/2];
Upvotes: 1
Reputation: 5268
Please find my working Code and screenshot
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *messageTableView;
@property (retain, nonatomic) NSMutableArray *datasourceArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.messageTableView registerNib:[UINib nibWithNibName:@"RestTimeTableViewCell" bundle:nil] forCellReuseIdentifier:@"RestTime"];
[self.messageTableView registerNib:[UINib nibWithNibName:@"ExerciseTimeTableViewCell" bundle:nil] forCellReuseIdentifier:@"ExerciseTime"];
self.messageTableView.separatorColor = [UIColor blackColor];
self.messageTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
self.datasourceArray = [[NSMutableArray alloc]init];
for (int i = 1; i <= 20; i++) {
[self.datasourceArray addObject:[NSString stringWithFormat:@"%d%d%d%d",i,i,i,i]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.datasourceArray.count * 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
// RestTime
if (indexPath.row % 2 == 1) {
return 40.0f;
}
// ExerciseTime
else {
return 65.0f;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row % 2 == 1) {
RestTimeTableViewCell *restTimeCell = (RestTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RestTime" forIndexPath:indexPath];
//Access you object from array like this
return restTimeCell;
}
else {
ExerciseTimeTableViewCell *exerciseTimecell = (ExerciseTimeTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ExerciseTime" forIndexPath:indexPath];
//Access you object from array like this
int index = (int)(indexPath.row / 2);
exerciseTimecell.exerciseName.text = [self.datasourceArray objectAtIndex:index];
return exerciseTimecell;
}
}
@end
Screenshot
Upvotes: 0
Reputation: 553
NSSet
just be a set, there's no order between objects. If you'd like to keep the order, you can use NSArray
(or NSMutableArray
) instead of NSSet
.
Upvotes: 0