I have some NSMutableArray in tableView which contain class Lesson
@interface Lesson : NSObject <NSCoding>{
NSString *time1;
NSString *time2;
NSString *predmet;
NSString *namPrepod;
NSString *zamet;
}
I move row in tableview………………………………………………………………………………………………………………..
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
if(segmentedControl.selectedSegmentIndex==0)
{
if(fromIndexPath.section==0)
{
NSMutableArray *r = [[Singleton sharedInstance].chetNedel.monday objectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday removeObjectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday insertObject:r atIndex:toIndexPath.row];
}
if(fromIndexPath.section==1)
{
NSMutableArray *r = [[Singleton sharedInstance].chetNedel.tuesday objectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday removeObjectAtIndex:fromIndexPath.row];
[[Singleton sharedInstance].chetNedel.monday insertObject:r atIndex:toIndexPath.row];
}
but when I move row in program I have got error on this lines
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
..........
if(segmentedControl.selectedSegmentIndex==0)
{
time1 = [[[UILabel alloc] init] autorelease];
time1.font = [UIFont fontWithName:@"Arial-BoldMT" size:13];
time1.tag = tagtime1;
time1.backgroundColor = [UIColor clearColor];
if(indexPath.section ==0)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.monday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==1)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.tuesday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==2)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.wednesday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==3)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.thirsday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==4)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.friday objectAtIndex:indexPath.row] time1];
if(indexPath.section ==5)
time1.text =[(Lesson *) [[Singleton sharedInstance].chetNedel.saturday objectAtIndex:indexPath.row] time1];
time1.frame = CGRectMake(8, 12, 300-124, 11);
...........
[cell.contentView addSubview:time1];
what i do wrong?
What error are you getting exactly? Looking at your code however I think the problem happens when you do the removeObjectAtIndex followed by insertObject:atIndex:. After you remove the object, the indexes of the array are recalculated and now you have 1 less object in your array. So if you then do the insertObject:atIndex without decreasing the index where you want to put it, you’re not going to end up with it where you want. I suggest you combine both lines into one and use exchangeObjectAtIndex:withObjectAtIndex: instead. Here’s updated code: