Iphone 重新加载表视图不工作
更新了我的问题Iphone 重新加载表视图不工作,iphone,ios,uitableview,reloaddata,Iphone,Ios,Uitableview,Reloaddata,更新了我的问题 - (void)updateCurrentLocation { switchview.on ? [self saveSettings:@"useLocation" :@"On"] : [self saveSettings:@"useLocation" :@"Off"]; NSLog(@"%@", [self loadSettings:@"useLocation"]); } @interface DOR_FiltersViewController : UIView
- (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;
我有一个设置页面,左侧显示设置名称,右侧显示当前设置(UITableViewCellStyleValue1
)。当您点击设置的单元格时,您会看到一张操作表,可以让您选择“全部查看”、“是”、“否”。我的目标是将他们选择的值放入单元格的右侧,以便他们可以看到所做的更改
行动单事件
由于之前的和
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;
}
更多信息
- (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;
这是我的.h文件的@接口:
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;
再来一次
- (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;
@interface-DOR\u-FiltersViewController:UIViewController
UITableView*tblView;
@属性(非原子,保留)UITableView*tblView;
@合成tblView;
另外,对于@implementation DOR_FiltersViewController
,我收到一条警告,说“未完成实现”。我不知道那个一般性的说法可能意味着什么。我试着查了一下,它似乎有什么意义
修复程序
首先,我发现tblView没有连接到我的表视图我必须右键单击表视图并将其拖到我的.h文件中,然后将其链接到tblView。我以为我已经这样做了。我现在觉得很傻。然后,对于@interface,我必须使用\uuuu-UITableView*tblView代码>,并在该@属性(弱、非原子)下显示IBOutlet UITableView*tblView代码>然后一切都正常了。两件事:table1settings
和table2settings
应该是NSMutableArray
,尽管根据您得到的错误,这不是问题所在
它看起来像是你们班的一个iVar
。您应该在ClickedButtonIndex:
试试这个:
- (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
对于索引
,应该有简单的nsuiger
编辑:
如果调用[self.tblView reloadData]
不启动任何cellForRowAtIndexPath:
调用,则self.tblView未被正确引用
编辑2:
确保-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
所在的类采用UITableViewDataSource
协议
您可以在.h
文件中执行此操作,例如:
@interface YourClass:UIViewController <UITableViewDataSource>
加
如果您使用的是任何UITableViewDelegate
方法,则必须将其加入到组合中:
@interface YourClass:UIViewController <UITableViewDataSource,UITableViewDelegate>
除非您更新了数据模型,否则重新加载表将无效-您需要包含的代码将是CellForRowatineXpath方法,以及在调用/解除操作表时运行的任何操作。请发布您的操作表回调和CellForRowatineXpath回调。您不使用的意思是什么cellForRowAtIndexPath:
?您不必(也不应该)调用cellForRowAtIndexPath:
,但必须实现它才能使UITableView
正常工作。我知道你已经从代码中删除了这个方法,这就是我为什么要问的原因。@rokjarc哦,这是一个打字错误(复制/粘贴)。我不再使用reloadrowsatindexpath
。@James:您可能已经将thisRow
声明为(NSUInteger*)
。它应该声明为simpleNSUInteger
,并按此处理(在使用它的代码的所有部分中)。另外:在调用replaceOBjectAtIndex:
之前,是否确定已填充数组。若索引大于数组中的对象数-1,则会出现错误。我在回答中添加了两条NSLog语句。试试看,然后把结果贴出来。@James:没问题。在这种情况下,self.tblView
没有被正确引用(即它没有指向tblView
),或者self不是tableViewDelegate
或tableViewDataSource
。您是通过编程方式还是通过IB创建tableView
?您是否设置了tblView.dataSource
和tblView.delegate
?你的.h文件中有
吗?在我的问题帖子底部添加了你最后评论的所有信息。我没有tblView.dataSource
或tblView.delegate
任何地方。是的,我非常感谢您的帮助。RE:Edit1。非常感谢。在我的例子中,是由脚本TableViewController导致的,该控制器的“视图”属性未连接到屏幕上的“tableView”对象。