加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – UITableView如何一次显示两个不同的数组?

发布时间:2020-12-14 17:22:05 所属栏目:百科 来源:网络整理
导读:下面的代码工作,但不是我希望.我希望当我点击UI按钮时,它自动更新UITableview中的新值而不是旧值.Below代码只有当我按下UI按钮时才有效,之后我滚动UITableview然后更新具有新值的UItableview. 在我的应用程序中,我使用UITableview作为我的mainclass.as图像的
下面的代码工作,但不是我希望.我希望当我点击UI按钮时,它自动更新UITableview中的新值而不是旧值.Below代码只有当我按下UI按钮时才有效,之后我滚动UITableview然后更新具有新值的UItableview.
在我的应用程序中,我使用UITableview作为我的mainclass.as图像的子类,如下所示

我在我的Mainclass中添加Tableview,就像这样“testingViewController”
在testingViewController.h中

#import "Inputtableview.h"
@interface testingViewController :UIViewController<UITableViewDelegate,UITableViewDataSource> {
     Inputtableview *inputview;
     IBOutlet UITableView *inputtbl; 
}
@end

在testingViewController.m中

- (void)viewDidLoad {
btn1bool=FALSE;
if (inputview == nil) {
    inputview = [[Inputtableview alloc] init];
}

[inputtbl setDataSource:inputview];
[inputtbl setDelegate:inputview];
inputview.view = inputview.tableView;
}

现在在Button动作方法中

-(IBAction)input:(id)sender
  {
  btn1bool=TRUE;
}

我的子类代码“inputtableview.m”如下所示

- (void)viewDidLoad {
 [super viewDidLoad];
listOfItems=[[NSMutableArray alloc] initWithObjects:@"Iceland",@"Greenland",@"Switzerland",@"Norway",@"New Zealand",@"Greece",@"Italy",@"Ireland",nil];

  array1 = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",nil] ;
 }

  #pragma mark -
  #pragma mark Table View datasource methods
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
    {
    if (btn1bool) {
        return [array1 count];
    }
else {
    return [listOfItems count];
}

[self.tableView reloadData];
   }


 -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
static NSString *CellIdentifier = @"CellIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

NSLog(@"Row: %i",indexPath.row);
if (btn1bool) {
    NSString *cellValue = [array1 objectAtIndex:indexPath.row];
    cell.text = cellValue;
}
else {
    NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
    cell.text = cellValue;
      }
   return cell;
 }

将提供任何帮助.

解决方法

只需输入以下代码:

[inputtbl reloadData];

您需要在项目中更改一些内容,但我认为此项目仅用于测试内容.

您希望在按下按钮后重新加载日期,因此您可以在IBAction中调用该方法.

-(IBAction)input:(id)sender
{
    btn1bool=TRUE;
    [inputview.tableView reloadData];
}

要在按下按钮时在两个数据源之间切换,您可以更改为以下代码行:btn1bool =!btn1bool;

(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    if (btn1bool) {
        return [array1 count];
    } else {
        return [listOfItems count];
    }
}

– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath是正确的

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读