更新了我的问题
我有一个设置页面,左侧显示设置名称,右侧显示当前设置 (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];
}
由于 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;
更新当前位置
- (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;
下然后一切工作了。
最佳答案
有两件事:table1settings
和table2settings
应该是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/