Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >iPad上未出现的iPhone表视图的奇怪行为

iPad上未出现的iPhone表视图的奇怪行为
EN

Stack Overflow用户
提问于 2011-08-23 07:17:22
回答 1查看 153关注 0票数 0

我已经构建了一个包含2个部分的分组表视图。

在每个部分中总是必须有一个选择,并且不能有更多选择。

在iPad上测试时,这是非常稳定的。

我最近开始为iPhone版本开发xib。

现在事情开始变得奇怪了。滚动选择时,所选内容到处飞,消失,有时在一个部分中有4个选择。

完全相同的代码可以在iPad上完美地运行。

有没有任何已知的bug可能会导致这种情况发生?

使用代码更新:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
        - (void)viewDidLoad
    {


         [super viewDidLoad];
         self.view.backgroundColor = [UIColor whiteColor]; 

    //Initialize the array.
    listOfItems = [[NSMutableArray alloc] init];

    NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:@"Baskerville", @"Palatino", @"Times New Roman", @"Verdana", nil];

    NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:@"Countries"];

    NSArray *countriesLivedInArray = [NSArray arrayWithObjects:@"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", @"My Text", nil];

    NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:@"Countries"];

    [listOfItems addObject:countriesToLiveInDict];
    [listOfItems addObject:countriesLivedInDict];

    //Set the title
    self.navigationItem.title = @"Countries";  

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];    

     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    //SET Font 
    NSString *neverOpendFonts1 = [prefs objectForKey:@"neverOpendFonts1"];  

    if (![neverOpendFonts1 isEqualToString:@"1"]) {


            lastIndexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];

            UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath1]; 

            newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

            NSString *lastIndexPathString1 = [NSString stringWithFormat:@"1"];

            [prefs setObject:lastIndexPathString1 forKey:@"lastIndexPath1"];

            NSString *fontName = [NSString stringWithFormat:@"Palatino"];

            [prefs setObject:fontName forKey:@"fontName"];

            neverOpendFonts1 = [NSString stringWithFormat:@"1"];

            [prefs setObject:neverOpendFonts1 forKey:@"neverOpendFonts1"];

            [prefs synchronize];


    }

    else
    {
        NSInteger row = [[prefs objectForKey:@"lastIndexPath1"] intValue];

        lastIndexPath1 = [NSIndexPath indexPathForRow:row inSection:0];

        UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath1]; 

        newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

    }

 //SET Font SIZE

    NSString *neverOpendFonts2 = [prefs objectForKey:@"neverOpendFonts2"];  

    if (![neverOpendFonts2 isEqualToString:@"1"]) {


        lastIndexPath2 =[NSIndexPath indexPathForRow:2 inSection:1];

        UITableViewCell *newCell = [myTableView cellForRowAtIndexPath:lastIndexPath2]; 

        newCell.accessoryType = UITableViewCellAccessoryCheckmark; 

        NSString *lastIndexPathString2 = [NSString stringWithFormat:@"2"];

        [prefs setObject:lastIndexPathString2 forKey:@"lastIndexPath2"];

        NSString *fontSize = [NSString stringWithFormat:@"24.0"];  

        [prefs setObject:fontSize forKey:@"fontSize"];

        neverOpendFonts2 = [NSString stringWithFormat:@"1"];

        [prefs setObject:neverOpendFonts2 forKey:@"neverOpendFonts2"];

        [prefs synchronize];

    }

    else
    {

        NSInteger row2 = [[prefs objectForKey:@"lastIndexPath2"] intValue];

        lastIndexPath2 = [NSIndexPath indexPathForRow:row2 inSection:1];

        UITableViewCell *newCell2 = [myTableView cellForRowAtIndexPath:lastIndexPath2]; 

        newCell2.accessoryType = UITableViewCellAccessoryCheckmark; 


    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //Number of rows it should expect should be based on the section
    NSDictionary *dictionary = [listOfItems objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    return [array count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return [listOfItems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set up the cell...

    //First get the dictionary object
    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"Countries"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

    if (indexPath.section == 0)
    {
        switch (indexPath.row) {
            case 0:
            {
             cell.textLabel.font = [UIFont fontWithName:@"Baskerville" size:24];   
            }
                break;
            case 1:
            {
              cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:24];    
            }
                break;
            case 2:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Times New Roman" size:24];  
            }
                break;
            case 3:
            {
               cell.textLabel.font = [UIFont fontWithName:@"Verdana" size:24];   
            }
                break;
        }

    }
    if (indexPath.section == 1) {

        switch (indexPath.row) {
            case 0:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:16];   
            }
                break;
            case 1:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:20];    
            }
                break;
            case 2:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:24];  
            }
                break;
            case 3:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:28];   
            }
                break;
            case 4:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:32];   
            }
                break;
            case 5:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:36];   
            }
                break;
            case 6:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:40];   
            }
                break;
            case 7:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:44];   
            }
                break;
            case 8:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:48];   
            }
                break;
            case 9:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:52];   
            }
                break;
            case 10:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:56];   
            }
                break;
            case 11:
            {
                cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:60];   
            }
                break;
        }


    }


    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // CGFloat result;

    if (indexPath.section == 0)
    {
        return 50;

    }
    if (indexPath.section == 1) {

        switch (indexPath.row) {
            case 0:
            {
                return 50;   
            }
                break;
            case 1:
            {
                return 50;    
            }
                break;
            case 2:
            {
                return 50;  
            }
                break;
            case 3:
            {
                return 50;   
            }
                break;
            case 4:
            {
                return 50;   
            }
                break;
            case 5:
            {
                return 50;   
            }
                break;
            case 6:
            {
                return 50;   
            }
                break;
            case 7:
            {
                return 54;   
            }
                break;
            case 8:
            {
                return 58;   
            }
                break;
            case 9:
            {
                return 62;
            }
                break;
            case 10:
            {
                return 66;
            }
                break;
            case 11:
            {
                return 70;  
            }
                break;
        }


    }
    return 50;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (section == 0) {
        return @"Font";
    }
    if (section == 1) {
        return @"Font Size";
    }
    else if (!section == 0 || !section == 1) {
        return @"Text";
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; 

    NSInteger newRow = [indexPath row];

    if (indexPath.section == 0) {

        NSString *fontName;

        switch (indexPath.row) {
            case 0:
            {
                fontName = [NSString stringWithFormat:@"Baskerville"];   

            }
                break;
            case 1:
            {
               fontName = [NSString stringWithFormat:@"Palatino"];    

            }
                break;
            case 2:
            {
               fontName = [NSString stringWithFormat:@"Times New Roman"];  

            }
                break;
            case 3:
            {
                fontName = [NSString stringWithFormat:@"Verdana"];  


            }
                break;
        }

        NSInteger oldRow = (lastIndexPath1 != nil) ? [lastIndexPath1 row] : -1; 

        if(newRow != oldRow) 
        { 
            newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath1]; 
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            lastIndexPath1 = indexPath; 
        }   


        NSInteger lastRow = lastIndexPath1.row;

        NSString *lastIndexPathString = [NSString stringWithFormat:@"%i", lastRow];

        [prefs setObject:lastIndexPathString forKey:@"lastIndexPath1"];

        [prefs setObject:fontName forKey:@"fontName"];

        [prefs synchronize];

        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }

    if (indexPath.section == 1) {

        NSString *fontSize;

        switch (indexPath.row) {
            case 0:
            {
               // cell.textLabel.font = [UIFont fontWithName:@"Palatino" size:16];  

                fontSize = [NSString stringWithFormat:@"16.0"];   

               // UITableViewCell *newCell =[tableView cellForRowAtIndexPath:indexPath];
               // newCell = [tableView  cellForRowAtIndexPath:indexPath];
               // newCell.accessoryType = UITableViewCellAccessoryCheckmark;

            }
                break;
            case 1:
            {
            fontSize = [NSString stringWithFormat:@"20.0"];   


            }
                break;
            case 2:
            {
            fontSize = [NSString stringWithFormat:@"24.0"];   


            }
                break;
            case 3:
            {
            fontSize = [NSString stringWithFormat:@"28.0"];   


            }
                break;
            case 4:
            {
            fontSize = [NSString stringWithFormat:@"32.0"];   


            }
                break;
            case 5:
            {
            fontSize = [NSString stringWithFormat:@"36.0"];   


            }
                break;
            case 6:
            {
            fontSize = [NSString stringWithFormat:@"40.0"];   


            }
                break;
            case 7:
            {
            fontSize = [NSString stringWithFormat:@"44.0"];   


            }
                break;
            case 8:
            {
            fontSize = [NSString stringWithFormat:@"48.0"];   


            }
                break;
            case 9:
            {
            fontSize = [NSString stringWithFormat:@"52.0"];   


            }
                break;
            case 10:
            {
            fontSize = [NSString stringWithFormat:@"56.0"];   


            }
                break;
            case 11:
            {
            fontSize = [NSString stringWithFormat:@"60.0"];   

            }
                break;
        }

        NSInteger oldRow = (lastIndexPath2 != nil) ? [lastIndexPath2 row] : -1; 

        if(newRow != oldRow) 
        { 
            newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath2]; 
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            lastIndexPath2 = indexPath; 
        }   


        NSInteger lastRow2 = lastIndexPath2.row;

        NSString *lastIndexPathString2 = [NSString stringWithFormat:@"%i", lastRow2];

        [prefs setObject:lastIndexPathString2 forKey:@"lastIndexPath2"];

        //NSString *lastIndexPathString = [NSString stringWithFormat:@"@%", lastIndexPath2];

        //[prefs setObject:lastIndexPathString forKey:@"lastIndexPath2"];


        [prefs setObject:fontSize forKey:@"fontSize"];

        [prefs synchronize];

        [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    }



}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-01 22:08:37

TSolved问题...我在iphonedevsdk.com上得到了答案...

我添加了

if([self.lastIndexPath1 isEqual:indexPath]) { cell.accessoryType = UITableViewCellAccessoryCheckmark; } else { cell.accessoryType = UITableViewCellAccessoryNone; }

到cellForRowAtIndexPath

并添加了

if(self.lastIndexPath1) { UITableViewCell* uncheckCell = [tableView cellForRowAtIndexPath:self.lastIndexPath1]; uncheckCell.accessoryType = UITableViewCellAccessoryNone; } UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; cell.accessoryType = UITableViewCellAccessoryCheckmark; self.lastIndexPath1 = indexPath; }

致didSelectRowAtIndexPath..。

我删除了旧的`/*

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    NSInteger oldRow = (lastIndexPath1 != nil) ? [lastIndexPath1 row] : -1; 

    if(newRow != oldRow) 
    { 
        newCell.accessoryType = UITableViewCellAccessoryCheckmark; 
        UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath1]; 
        oldCell.accessoryType = UITableViewCellAccessoryNone;
        lastIndexPath1 = indexPath; 
    }   

           */`

来自didSelectRow,现在一切都完美地工作了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7157491

复制
相关文章
delete的奇怪行为
F的实例拥有一个value属性,但不希望在new的时候就初始化属性值(因为这个值不一定用得到,而且计算成本比较高,或者new的时候还不一定能算出来),那么自然想到通过定义getter来实现“按需计算”:
ayqy贾杰
2019/06/12
2.4K0
2833 奇怪的梦境 未AC
2833 奇怪的梦境 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很多按钮,还有一个屏幕,上面显示了一些信息。屏幕上说,要将所有按钮都按下才能出去,而又给出了一些信息,说明了某个按钮只能在另一个按钮按下之后才能按下,而没有被提及的按钮则可以在任何时候按下。可是Aiden发现屏幕上所给信息似乎有矛盾,请你来帮忙判断。 输入描述 Input Description 第
attack
2018/04/12
5820
AnyGo for Mac(在iPhone / iPad上轻松模拟GPS位置)
AnyGo是一款适用于Mac系统的虚拟定位工具,它可以模拟GPS定位,让用户随意选择想要定位的地点,并实现手机等移动设备的定位修改。使用AnyGo,用户可以欺骗任何位置服务,包括在社交媒体上共享的位置、游戏中的位置以及其他应用程序中的位置,而不需要实际到达那个地方。
对影三人
2023/03/20
2.3K0
AnyGo for Mac(在iPhone / iPad上轻松模拟GPS位置)
CSS3 Media Queries在iPhone4和iPad上的运用
CSS3 Media Queries的介绍在本站上的介绍已有好几篇文章了,但自己碰到的问题与解决的文章还是相对的较少。同一个项目,为了实现iPhone和iPad横板与竖板的风格,让我还是头疼了一翻。
李维亮
2021/07/09
7960
CSS3 Media Queries在iPhone4和iPad上的运用
使iPhone也可以拥有iPad的pop效果
iOS 8 之后新增了 UIPresentationController 控制器,用于所有的界面转场。在以前,popViewController只适用于iPad。而现在使用UIPopoverPresentationController不再需要对设备进行判断了。 这个控制器的用法我还在摸索中,今天先上一段代码,做到在iPhone像iPad中的pop弹出视图效果。 UIPopoverPresentationController不需要你去创建。苹果官方文档:In nearly all cases, you us
Raindew
2018/06/14
8380
iPhone 11系列/新iPad消息汇总
iPhone 11系列/新iPad消息汇总: 外观基本不变,大小和刘海基本一致。部分配色会加入哑光效果,并且采用新的防碎玻璃技术,减少跌落时背盖碎裂的可能。
小贝壳
2020/03/05
5110
AnyGo for Mac(在iPhone / iPad上轻松模拟GPS位置) 6.0.0免激活版
AnyGo for Mac是一款一键将iPhone的Gps位置更改为任何位置的强大软件。AnyGo使您只需单击一下鼠标,即可将iPhone的Gps位置传送到您在世界上选择的任何目的地!只需输入地址或所需位置的特定坐标即可。
一小朵
2023/02/17
9650
AnyGo for Mac(在iPhone / iPad上轻松模拟GPS位置) 6.0.0免激活版
iPhone,iPad 等常用设备的 CSS3 Media Queries
CSS3 中的 Media Queries 可以让我们设置不同类型的媒体条件,并根据对应的条件,给相应符合条件的媒体调用相对应的样式表。现在最常见的一个例子,就是可以同时给 PC 的大屏幕和移动设备设置不同的样式表。这功能是非常强大的,它可以让我们定制不同的分辨率和设备,并在不改变内容的情况下,制作的网页在不同的分辨率和设备下都能显示正常,并且不会因此而丢失样式。
Denis
2023/04/14
4270
iPhone,iPad 等常用设备的 CSS3 Media Queries
增强现实何时会出现在iPhone上
据外媒报道,近日,苹果公司收购了增强现实初创公司Metaio。根据Metaio网站的介绍获悉,这是一家增强现实和电脑视觉的先锋公司,它开发了一套增强现实创作工具--Metaio Creator。用户可
机器人网
2018/04/19
4570
增强现实何时会出现在iPhone上
Iphone 视图跳转方法总结
1.AddInfo *control = [[AddInfo alloc] init];   [self presentModalViewController:control animated:YES];   [control release];   描述:通过事件进行跳转   [self dismissModalViewControllerAnimated:YES];   描述:通过事件进行返回。   2.[self.navigationController pushViewController:subT
阿新
2018/04/12
6620
如何在iPhone和iPad上隐藏IP地址,保护个人隐私信息
当我们用Safari浏览器访问网页时,很多网站都会追踪IP地址并获取我们的个人信息,以便进行精准营销。
莉莉的碎碎念
2021/09/12
3.3K0
如何在iPhone和iPad上隐藏IP地址,保护个人隐私信息
点击加载更多

相似问题

iPad上奇怪的网站行为

19

iPad上NavigationLink的奇怪行为

118

iPad上奇怪的约束行为

12

iPhone 6+上堆栈视图的奇怪行为

22

Autolayout在Ipad上的奇怪行为

13
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文