【问题标题】:How to load tableview based on button values with custom cells?如何使用自定义单元格根据按钮值加载表格视图?
【发布时间】:2016-05-04 11:38:04
【问题描述】:

在我的应用程序中,我想显示一个基于按钮标签的表格视图(自定义单元格),当我点击我的第一个按钮时,它显示空表格视图,点击第二个按钮它显示第一个按钮数据。 这是我到目前为止的代码,我试过了, 我在 ViewDidLoad 方法中调用我的 IBAction 按钮,

- (void)viewDidLoad
{
 value1=0;
 [self details:nil];
}

这是我的 IBAction 方法,

-(IBAction)details:(id)sender
{
  value1 = 0;
  detaiLine.hidden = NO;
  rewardLine.hidden = YES;
  [testTableView reloadData];
}

这是我的 tableViewDataSource 方法,

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
   return 1;
 }
 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return responseArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (value1 == 0)
{
    return 104;
}
if(value1 == 1)
{
    return 180;
}
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  if (value1 == 0)
        {
            static NSString *cellId = @"Cell1";
            ContestDetailCell1 * cell = (ContestDetailCell1 *)[contestTableView dequeueReusableCellWithIdentifier:cellId];
            if (cell == nil)
            {
                NSArray *myNib;
                myNib = [[NSBundle mainBundle]loadNibNamed:@"ContestDetailCell1" owner:self options:nil];
                cell = [myNib lastObject];
            }
      }
if(value1 == 1)
{
    static NSString *cellIdentifier = @"Cell2";
    ContestDetailCell2 *cell = (ContestDetailCell2 *)[contestTableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        NSArray *myNib;
        myNib = [[NSBundle mainBundle]loadNibNamed:@"ContestDetailCell2" owner:self options:nil];
        cell =[myNib objectAtIndex:0];
    }
}

我的代码有什么问题。

【问题讨论】:

  • In viewDidLoad: 你已经发布了 value=0;,是代码中的错字还是类似的错误,因为当你的表加载时 value1 根本没有设置第一次。另外,我猜在 reloadData 中不会调用 heightForRowAtIndexPath。请检查。
  • @user3300864 我会检查并通知你
  • @user3300864 heightForRowAtIndexPath 正在调用。
  • 你的笔尖可能没有初始化。你在注册你的 xib 吗?

标签: objective-c uitableview uibutton custom-cell


【解决方案1】:

您没有将标签值分配给 value1 变量。试试这段代码,

-(IBAction)details:(id)sender
{
  value1 = sender.tag;
  detaiLine.hidden = NO; 
  rewardLine.hidden = YES;
  [testTableView reloadData];
}

【讨论】:

  • 我没有使用标签。
【解决方案2】:

我认为你没有正确实现你的逻辑。我不明白你为什么从 viewdidload 调用 IBAction。 请将此作为解决方法,

  1. 为xib中的每个按钮添加标签或使用代码。
  2. 在您的 IBAction 方法中获取标签并识别按钮如下,

    -(IBAction)details:(id)sender { value1 = 发件人.tag; \\你的剩余代码在这里 }

  3. 使用此值 1 在表格视图中加载数据。

希望这会对您有所帮助。如果我错了,请纠正我。 :P

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多