程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-在UITableView中添加數組首尾處的UIbutoton

ios-在UITableView中添加數組首尾處的UIbutoton

編輯:編程綜合問答
在UITableView中添加數組首尾處的UIbutoton

問題:我要在UITableView中添加UIButton,並且只加在第一個和最後一個數組上,不知道應該怎麼實現?我知道可以用tableFooterView添加按鈕,但是不知道怎麼樣實現添加到指定數組位置中。請多多指教,謝謝

我的代碼:

- (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的每個單元裡面接受到Button。

最佳回答:


先修改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;
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved