所以我得到了这个名为tableDateArray的数组设置,它在SubClass.h中定义。现在我试图改变数组的数据是否一个按钮被选中。
我在MainView.m中的代码:
- (IBAction)dateSpecifiedButtonTouch:(id)sender {
if (monthly.enabled == NO){
NSLog(@"no");
tableDateArray = [NSArray arrayWithObjects:
@"January 2012",
@"February 2012",
@"March 2012",
@"April 2012",
@"May 2012",
@"June 2012",
@"July 2012",
@"August 2012",
@"September 2012",
@"Octobre 2012",
@"November 2012",
@"December 2012",
@"All months",
nil];
}
if (monthly.enabled == YES){
NSLog(@"yes");
tableDateArray = [NSArray arrayWithObjects:
@"2002",
@"2003",
@"2004",
@"2005",
@"2006",
@"2007",
@"2008",
@"2009",
@"2010",
@"2011",
@"2012",
@"All years",
nil];
}}
所以NSlog正在跟踪正确的值,所以这不是问题。但是我的数组将被首先选择的状态填充,并且拒绝更改数据。我肯定我忽略了一些简单的事情。
每个请求添加:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell"; // unused method of type cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [tableDateArray objectAtIndex:indexPath.row];
return cell;
}
老实说,我不认为我叫reloaddata
从提供的代码到现在,它看起来很好,没有理由说为什么不工作
可能这是更正确的方式来写代码
- (IBAction)dateSpecifiedButtonTouch:(id)sender {
if (monthly.enabled)
{
NSLog(@"yes");
tableDateArray = [NSArray arrayWithObjects:
@"2002",
@"2003",
@"2004",
@"2005",
@"2006",
@"2007",
@"2008",
@"2009",
@"2010",
@"2011",
@"2012",
@"All years",
nil];
}
else
{
NSLog(@"no");
tableDateArray = [NSArray arrayWithObjects:
@"January 2012",
@"February 2012",
@"March 2012",
@"April 2012",
@"May 2012",
@"June 2012",
@"July 2012",
@"August 2012",
@"September 2012",
@"Octobre 2012",
@"November 2012",
@"December 2012",
@"All months",
nil];
}
[table reloadData];
}
编辑:
为了达到这个目的我们设有两个视图控制器VC1 VC2
在VC1中,您正在更新值,而在VC2中,您使用名为inputArray的数据源数组更新表。
- 所以在VC1中,当按钮按下
- 获取vc2 实例将数组赋值给VC2instance.inputArray=tableDataArray;
- 调用reloadData表刷新
天哪…很多答案都错了。
只需从IBAction调用[yourTableViewOutlet reloadData]
,其中每月按钮被触摸。
如
- (IBAction)dateSpecifiedButtonTouch:(id)sender {
...
...
...
...
[yourTableViewOutlet reloadData];
}
当然你会想要改变" yourTableViewOutlet
"的真实名称,你已经连接到一个出口表视图
- (IBAction)dateSpecifiedButtonTouch:(id)sender {
if (monthly.enabled == NO){
NSLog(@"no");
tableDateArray = [[NSMutableArray alloc ]initWithObjects:
@"January 2012",
@"February 2012",
@"March 2012",
@"April 2012",
@"May 2012",
@"June 2012",
@"July 2012",
@"August 2012",
@"September 2012",
@"Octobre 2012",
@"November 2012",
@"December 2012",
@"All months",
nil];
}
if (monthly.enabled == YES){
NSLog(@"yes");
tableDateArray = [[NSMutableArray alloc ]initWithObjects:
@"2002",
@"2003",
@"2004",
@"2005",
@"2006",
@"2007",
@"2008",
@"2009",
@"2010",
@"2011",
@"2012",
@"All years",
nil];
}}
- (IBAction)dateSpecifiedButtonTouch:(id)sender {
//maybe toggle monthly.enabled here?
[yourTableView reloadData];
}
static NSArray * tableDateArray = nil;
static NSArray * tableYearArray = nil;
+(void)initialize
{
if(self == [MyCLass class])
{
tableDateArray = [[NSMutableArray alloc ]initWithObjects:
@"January 2012",
@"February 2012",
@"March 2012",
@"April 2012",
@"May 2012",
@"June 2012",
@"July 2012",
@"August 2012",
@"September 2012",
@"Octobre 2012",
@"November 2012",
@"December 2012",
@"All months",
nil];
tableYearArray = [[NSMutableArray alloc ]initWithObjects:
@"2002",
@"2003",
@"2004",
@"2005",
@"2006",
@"2007",
@"2008",
@"2009",
@"2010",
@"2011",
@"2012",
@"All years",
nil];
}
}
//all of your tableview delegates/data sources:
{
NSArray * dataSource;
dataSource = monthly.enabled ? tableDateArray : tableYearArray;
//use datasource as the datasource
}
我按照建议使用reloadData
修复了我的问题。然而,reloadData
必须在viewWillAppear中调用,否则它会拒绝更新我的tableView。
- (void)viewWillAppear:(BOOL)animated{
[self.tableView reloadData];
}