UITableView 移动行

发布于 2024-12-29 17:58:14 字数 2768 浏览 3 评论 0原文

的课程

@interface Lesson : NSObject <NSCoding>{

    NSString *time1;
    NSString *time2;
    NSString *predmet;
    NSString *namPrepod;
    NSString *zamet;
}

我在 tableView 中有一些 NSMutableArray,其中包含我在 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];
    }

但是当我在程序中移动行时我在这行有错误

- (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];

我做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的韩爷 2025-01-05 17:58:14

你到底得到了什么错误?但是,查看您的代码,我认为当您先执行removeObjectAtIndex,然后执行insertObject:atIndex: 时,就会出现问题。删除对象后,将重新计算数组的索引,现在数组中少了 1 个对象。因此,如果您随后执行 insertObject:atIndex 而不减少要放置它的索引,那么您最终不会将其放置在您想要的位置。我建议您将这两行合并为一行并使用 ExchangeObjectAtIndex:withObjectAtIndex: 代替。这是更新的代码:

    if(fromIndexPath.section==0)
{
   [[Singleton sharedInstance].chetNedel.monday exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
}
if(fromIndexPath.section==1)
{
    [[Singleton sharedInstance].chetNedel.tuesday exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
}

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:

    if(fromIndexPath.section==0)
{
   [[Singleton sharedInstance].chetNedel.monday exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
}
if(fromIndexPath.section==1)
{
    [[Singleton sharedInstance].chetNedel.tuesday exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
}
薄荷→糖丶微凉 2025-01-05 17:58:14

我没有找到你的错误,但我认为你必须使用自定义 UITableViewCell 而不是在每一行添加 UILabele ...因为单元格被回收并且 time1 可能已经存在。

i don't find your error but i think you must use a custom UITableViewCell instead of add an UILabele at every row...because cell are recycled and time1 may already exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文