iphone - 重新加载 TableView 不起作用

标签 iphone ios uitableview reloaddata

更新了我的问题

我有一个设置页面,左侧显示设置名称,右侧显示当前设置 (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

索引应该有简单的NSUinteger

编辑:

如果调用 [self.tblView reloadData]; 未启动任何 cellForRowAtIndexPath: 调用,则 self.tblView 未正确引用。

编辑2:

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

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

@interface YourClass:UIViewController <UITableViewDataSource>

您应该让table知道她的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 - 常规低通滤波器和自适应低通滤波器有什么区别?

ios - 如何同时弹出模态视图和上一个导航 Controller View ?

ios - 实现 UIActivityItemSource 协议(protocol)

ios - Xamarin 在 iOS 中使用自定义 UITableViewCell 时抛出 NullReferenceException

ios - 重绘时未清除 UIBezierPath

iphone - 如何获取日期和时间 (MM/dd/yyyy hh :MM a) format?

iphone - 如何检查您的 iPhone 联系人列表中的人是否安装了应用程序?

iOS 截图实时相机预览

ios - 扩展 RCTConvert 以提供自定义函数类型

ios - 将数据从一个 UITableView 传递到 didSelectRow 中的另一个 UITableView?