更新了我的问题
我有一个设置页面,我在左侧显示设置名称,右侧显示当前设置(UITableViewCellStyleValue1
)。当您点击设置的单元格时,您会看到一个操作表,可让您选择“查看全部”,“是”,“否”。我的目标是将他们选择的值放入单元格的右侧,以便他们可以看到更改。
活动表事件
- (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];
}
由于Before
和After
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;
updateCurrentLocation
- (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连接到我的表视图。 ---我必须右键单击表视图并将其拖到我的.h文件并将其链接到tblView。我以为我已经做过了。我觉得现在非常愚蠢。然后,对于@interface,我必须使用__weak IBOutlet UITableView *tblView;
,然后在@property (weak, nonatomic) IBOutlet UITableView *tblView;
下,这一切都有效。
答案 0 :(得分:2)
有两件事:table1settings
和table2settings
应该是NSMutableArray
但是根据你得到的错误,这不是问题。
看来thisVal
是您班级的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
应该有简单的index
。
编辑:
如果调用[self.tblView reloadData];
未启动任何cellForRowAtIndexPath:
次调用,则无法正确引用self.tblView。
编辑2:
确保- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
所在的班级采用UITableViewDataSource
协议。
您可以在.h
文件中执行此操作,例如:
@interface YourClass:UIViewController <UITableViewDataSource>
你应该让table
知道她dataSource
是谁。在代码中,您设置了
self.tblView = thatTable;
添加
self.tblView.dataSource = self;
如果你使用任何UITableViewDelegate
方法,你必须把它扔进混合中:
@interface YourClass:UIViewController <UITableViewDataSource,UITableViewDelegate>
和
self.tblView.delegate = self;