【问题标题】:iPhone sdk how to add UIButton with first and last array count in UITableView?iPhone sdk 如何在 UITableView 中添加具有第一个和最后一个数组计数的 UIButton?
【发布时间】:2012-12-17 08:51:40
【问题描述】:

我需要在我的UITableView 中添加一个UIButton,仅包含我的第一个和最后一个数组计数。我发现我们可以使用tableFooterView,在我们的表格视图下方添加取消按钮。但是我怎样才能在我的 tableview 上实现这一点,并且只使用第一个和最后一个数组值呢?这是我的代码,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath   *)indexPath
{ 

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Adding a UIButton in last row

    NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
    NSLog(@"lastSectionIndex:%d",lastSectionIndex);

    // Then grab the number of rows in the last section
    NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
    NSLog(@"lastRowIndex:%d",lastRowIndex);

    // Now just construct the index path
    NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
    NSLog(@"last index:%@",pathToLastRow);

    if (pathToLastRow.row == lastRowIndex) 
    {
        NSLog(@"row enters");

        checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
        [checkButton1 addTarget:self
                         action:@selector(customActionPressed:)
               forControlEvents:UIControlEventTouchDown];
        [checkButton1 setBackgroundImage:[[UIImage imageNamed:@"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
        editTable.tableFooterView = checkButton1;
        [cell addSubview:checkButton1]; 
    }

现在,我在 tableview 的每个单元格中都收到了按钮。如何仅将按钮提供给我的第一行和最后一行数组值?提前致谢。

【问题讨论】:

  • 而不是在 CellforRow 中创建 Button 您可以在 ViewDidload 中创建并将其附加到表格的 FooterView 中。

标签: iphone ios uitableview uibutton


【解决方案1】:

将您的 if 条件更改为,

if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
{

它看起来像这样,

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        UIButton *checkButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
        checkButton1.tag = 100;//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
        [checkButton1 setFrame:CGRectMake(200, 0, 168, 168)];
        [checkButton1 addTarget:self
                     action:@selector(customActionPressed:)
           forControlEvents:UIControlEventTouchDown];
        [checkButton1 setBackgroundImage:[[UIImage imageNamed:@"Up Arrow.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
        [cell addSubview:checkButton1]; 
    }

   //Adding a UIButton in last row
   NSInteger lastSectionIndex = [editTable numberOfSections] - 1;
   NSLog(@"lastSectionIndex:%d",lastSectionIndex);

   // Then grab the number of rows in the last section
   NSInteger lastRowIndex = [editTable numberOfRowsInSection:lastSectionIndex] - 1;
   NSLog(@"lastRowIndex:%d",lastRowIndex);

   // Now just construct the index path
   NSIndexPath *pathToLastRow = [NSIndexPath indexPathForRow:lastRowIndex inSection:lastSectionIndex];
   NSLog(@"last index:%@",pathToLastRow);

   UIButton *checkButton1 = (UIButton *)[cell viewWithTag:100];//not recommended, I would suggest to use custom UITableViewCell class and add this button as subview inside its init method
   if ((lastSectionIndex == indexPath.section && lastRowIndex == indexPath.row ) || (indexPath.section == 0 && indexPath.row == 0 ))
    {
       checkButton1.hidden = NO;
    } else {
       checkButton1.hidden = YES;
    }

您不必在 .h 文件中声明 checkButton1。如上所示,使其成为局部变量。然后您可以设置隐藏属性以在不同的单元格中隐藏/显示。除了上述操作之外,您还可以在自定义UITableViewCell 类中创建此按钮,并将隐藏属性设置为cell.checkButton1.hidden = YES。为此,您需要将 UITableViewCell 子类化。

【讨论】:

  • 现在按钮在我的表格视图下方。
  • @Saranya,很高兴知道这一点。如果这有帮助,请接受。 :)
【解决方案2】:

您可以在 ViewDidload 中创建而不是在 CellforRow 中创建 Button 并将其附加到表格的 FooterView 中。

UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [aButton setTitleColor:[UIColor colorWithWhite:0.0 alpha:0.56] forState:UIControlStateDisabled];
    [aButton setBackgroundImage:[[UIImage imageNamed:@"test.png"] stretchableImageWithLeftCapWidth:kButtonSliceWidth topCapHeight:0] forState:UIControlStateNormal];
    [aButton setTitle:@"Click me" forState:UIControlStateNormal];
    [aButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]];
    [aButton setFrame:CGRectMake(10.0, 15.0, 300.0, 44.0)];
    [self.tableView setTableFooterView:aButton];

你可以对 headerview 做同样的事情。否则你可以使用Viewforheader 方法

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{        
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; // x,y,width,height

    UIButton *reportButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];    
    reportButton.frame = CGRectMake(80.0, 0, 160.0, 40.0); // x,y,width,height
    [reportButton setTitle:@"rep" forState:UIControlStateNormal];
    [reportButton addTarget:self 
                     action:@selector(buttonPressed:)
           forControlEvents:UIControlEventTouchDown];        

    [headerView addSubview:reportButton];
    return headerView;    
}

【讨论】:

    【解决方案3】:

    您应该将第一个按钮添加到您的表格视图的页眉中,而其他按钮应该添加到您的页脚视图中的 tableView 部分

    【讨论】:

      猜你喜欢
      • 2012-01-09
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 2019-09-19
      • 2013-01-14
      相关资源
      最近更新 更多