簡體   English   中英

將UIButton添加到UItableview單元格

[英]Adding UIButton to UItableview cells

我有一個分組的tableview包含3個部分,我想為每個部分添加不同的按鈕,我在第一部分有5個單元格,我想在第二部分只添加3個unbuttons,3r和第4個單元格。我的代碼看起來像這樣

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
        return [_arayfirstrow count];
    else if(section == 1)
        return [_arraysecondrow count];
    else  if(section == 2)
        return [_arraythirdrow count];
}
   // Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";
   // static NSUInteger nTag = 100;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.font = [UIFont fontWithName:@"Georgia"size:20.0];



    }

    // Set up the cell...
    if(indexPath.section == 0){
            cell.textLabel.text = [_arayfirstrow objectAtIndex:indexPath.row];
        NSString *imageName = [_arayfirstrowimg objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:imageName];

**//when i add below code for inserting button on the first section ,it successfully adds the button,but i don't need button for 1st and 5th cell..ans also it crashes when i tap the button for 2 or three time**

  CGRect buttonRect = CGRectMake(210, 25, 65, 25);
     UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = buttonRect;
        // set the button title here if it will always be the same
        [button setTitle:@"Action" forState:UIControlStateNormal];
        button.tag = 1;
        [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];

        [cell.contentView insertSubview:button atIndex:0];



       // [usernamebutton release];

    }else if(indexPath.section == 1){
        cell.textLabel.text = [_arraysecondrow objectAtIndex:indexPath.row];
    }
    else if(indexPath.section == 2)
    {
        cell.textLabel.text = [_arraythirdrow objectAtIndex:indexPath.row];

    }
    return cell;
}

有沒有解決方案來解決這個問題。 提前致謝。

你可以嘗試這樣:

if(indexPath.section == 0)
{
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3)
    {
        CGRect buttonRect = CGRectMake(210, 25, 65, 25);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = buttonRect;
        // set the button title here if it will always be the same
        [button setTitle:@"Action" forState:UIControlStateNormal];
        button.tag = 1;
        [button addTarget:self action:@selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubView:button atIndex:0];
    }
}

你有沒有定義myAction:的方法myAction: 如果沒有,它可能會使您的應用程序崩潰。

首先,這很容易:

將UITableViewCell的子類添加到項目中,插入標題並在委托方法中添加單元格,如下所示:

static NSString *CellIdentifier = @"SpecialCell";
// static NSUInteger nTag = 100;
YourTableViewCellClass *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (specialCell == nil) {
    specialCell = [[[YourTableViewCellClass alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

在您新的Cell類中,根據需要添加按鈕和視圖

[self addSubview:button];

這是您的特殊細胞,第一部分是第2,第3和第4細胞的細胞。

就像你已經做的那樣,以與上面相同的方式初始化標准單元格。 現在來了魔術:

if(indexPath.section == 0){
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3){
        //add text to buttons of your specialCell
        //add color...
        return specialCell;
    } else {
        return cell;
    }
}

只是一個小概述,它是如何完成的。 希望有所幫助。

if(indexPath.section == 0)
{
    if(indexPath.row == 1||indexPath.row == 2||indexPath.row == 3)
    {    

UIButton *deletebtn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
            [deletebtn setFrame:CGRectMake(170,5, 25, 25)];
            deletebtn.tag=indexPath.row;
            [deletebtn setImage:[UIImage imageNamed:@"log_delete_touch.png"] forState:UIControlStateNormal];
            [deletebtn addTarget:self action:@selector(DeleteRow:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:deletebtn];
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM