草庐IT

iphone - 重新加载 TableView 不起作用

coder 2024-01-21 原文

更新了我的问题

我有一个设置页面,左侧显示设置名称,右侧显示当前设置 (UITableViewCellStyleValue1)。当您点击设置的单元格时,您会看到一个操作表,您可以在其中选择“查看全部”、"is"、“否”。我的目标是将他们选择的值放入单元格的右侧,以便他们可以看到所做的更改。

操作表事件

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        thisVal = @"Show All";
        NSLog(@"Button 0");
    } else if (buttonIndex == 1) {
        thisVal = @"Yes";
        NSLog(@"Button 1");
    } else if (buttonIndex == 2) {
        thisVal = @"No";
        NSLog(@"Button 2");
    } else if (buttonIndex == 3) {
        NSLog(@"Button 3");
    }

    [self saveSettings:thisKey :thisVal];

    NSLog(@"Before: %@",[table2settings objectAtIndex:(NSUInteger)thisRow]);

    if (thisSection == 0){
        [table1settings replaceObjectAtIndex:(NSUInteger)thisRow withObject:thisVal];
    }else{
        [table2settings replaceObjectAtIndex:(NSUInteger)thisRow withObject:thisVal];
    }

    NSLog(@"After: %@",[table2settings objectAtIndex:(NSUInteger)thisRow]);

    [self.tblView reloadData];
}

由于 BeforeAfter NSlog,我可以看到实际数组正在更新。但是 tblView 不会重新加载。数据。

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier;
    if (indexPath.row == 0 && indexPath.section == 0){
        CellIdentifier = @"CellWithSwitch";
    }else{
        CellIdentifier = @"PlainCell";
    }


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    if (indexPath.section == 0){
        [[cell textLabel] setText:[table1labels objectAtIndex:indexPath.row]];
        if (indexPath.row == 0 && indexPath.section == 0){
            BOOL switchOn;
            if ([[table1settings objectAtIndex:indexPath.row] isEqualToString: @"On"]){
                switchOn = YES;
            }else{
                switchOn = NO;
            }

            switchview = [[UISwitch alloc] initWithFrame:CGRectZero];
            [switchview setOn:switchOn animated:YES];
            [switchview addTarget:self action:@selector(updateCurrentLocation) forControlEvents:UIControlEventValueChanged];
            cell.accessoryView = switchview;
        }else{

            if (![[table1settings objectAtIndex:indexPath.row] isEqualToString: @""]){
                [[cell detailTextLabel] setText:[table1settings objectAtIndex:indexPath.row]];
            }else{
                [[cell detailTextLabel] setText:@""];
            }
        }
    }else{
        if (![[table2settings objectAtIndex:indexPath.row] isEqualToString: @""]){
            [[cell detailTextLabel] setText:[table2settings objectAtIndex:indexPath.row]];
        }else{
            [[cell detailTextLabel] setText:@""];
        }
        [[cell textLabel] setText:[table2labels objectAtIndex:indexPath.row]];

    }

    return cell;
}

更多信息

这是我的 .h 文件的@interface:

NSMutableArray *table1settings;
NSMutableArray *table2settings;

在那之下:

@property (nonatomic, retain) NSMutableArray *table1labels;
@property (nonatomic, retain) NSMutableArray *table2labels;

还有我的 .m 文件:

@synthesize table1settings;
@synthesize table2settings;

更新当前位置

- (void)updateCurrentLocation {
    switchview.on ? [self saveSettings:@"useLocation" :@"On"] : [self saveSettings:@"useLocation" :@"Off"];
    NSLog(@"%@", [self loadSettings:@"useLocation"]);
}

再来一次

@interface DOR_FiltersViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate>
UITableView *tblView;
@property (nonatomic, retain) UITableView *tblView;
@synthesize tblView;

另外,对于 @implementation DOR_FiltersViewController,我收到一条警告说“未完成的实现”。我不知道该通用语句可能意味着什么。尝试查找它,它几乎似乎意味着什么。

修复

首先,我发现我没有将 tblView 连接到我的 TableView 。 -.- 我必须右键单击 TableView 并将其拖到我的 .h 文件并将其链接到 tblView。我以为我已经做到了。我现在觉得很傻。然后,对于@interface,我不得不使用__weak IBOutlet UITableView *tblView;,在那个@property (weak, nonatomic) IBOutlet UITableView *tblView;下成功了。

最佳答案

两件事:table1settingstable2settings 应该是 NSMutableArray 虽然根据错误你得到这不是问题。

看起来 thisVal 是您类(class)的一个 iVar。您应该在 clickedButtonAtIndex:

中分配它

试试这个:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

    NSString *thisVal; //this line was added

    if (buttonIndex == 0) {
        thisVal = @"Show All";
        NSLog(@"Button 0");
    } else if (buttonIndex == 1) {
        thisVal = @"Yes";
        NSLog(@"Button 1");
    } else if (buttonIndex == 2) {
        thisVal = @"No";
        NSLog(@"Button 2");
    } else if (buttonIndex == 3) {
        NSLog(@"Button 3");
    }

    [self saveSettings:thisKey :thisVal];

    if (thisSection == 0){
        NSLog(@"thisRow is %d and table1settings has %d elements", thisRow, [table1settings count]);
        [table1settings replaceObjectAtIndex:(NSUInteger)thisRow withObject:thisVal];
    }else{
        NSLog(@"thisRow is %d and table2settings has %d elements", thisRow, [table2settings count]);
        [table2settings replaceObjectAtIndex:(NSUInteger)thisRow withObject:thisVal];
    }
    [self.tblView reloadData];
}

当然还有删除 thisVal 的其他实现(可能在 @interface 部分)。

另请注意,replaceObjectAtIndex: 具有以下结构:

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject

对于 index 应该有简单的 NSUinteger

编辑:

如果调用 [self.tblView reloadData]; 没有启动任何 cellForRowAtIndexPath: 调用,那么 self.tblView 没有被正确引用。

编辑 2:

确保- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath所在类采用UITableViewDataSource协议(protocol)。

您在 .h 文件中执行此操作,例如:

@interface YourClass:UIViewController <UITableViewDataSource>

而且你应该让知道她的dataSource是谁。在代码中,你设置的地方

self.tblView = thatTable;

添加

self.tblView.dataSource = self;

如果您正在使用任何 UITableViewDelegate 方法,您必须将其加入混合:

@interface YourClass:UIViewController <UITableViewDataSource,UITableViewDelegate>

self.tblView.delegate = self;

关于iphone - 重新加载 TableView 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9907464/

有关iphone - 重新加载 TableView 不起作用的更多相关文章

  1. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  2. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  3. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  4. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  5. ruby - 在 Ruby 中重新分配常量时抛出异常? - 2

    我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案

  6. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  7. ruby-on-rails - 从应用程序中自定义文件夹内的命名空间自动加载 - 2

    我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty

  8. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  9. ruby - 将全局 $stdout 重新分配给控制台 - ruby - 2

    我正在尝试将$stdout设置为临时写入一个文件,然后返回到一个文件。test.rb:old_stdout=$stdout$stdout.reopen("mytestfile.out",'w+')puts"thisgoesinmytestfile"$stdout=old_stdoutputs"thisshouldbeontheconsole"$stdout.reopen("mytestfile1.out",'w+')puts"thisgoesinmytestfile1:"$stdout=old_stdoutputs"thisshouldbebackontheconsole"这是输出。r

  10. ruby - Faye WebSocket,关闭处理程序被触发后重新连接到套接字 - 2

    我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d

随机推荐