2016-06-23 2 views
1

Я новичок в этом, поэтому у меня возникают проблемы. Моя цель слишком проста, как и название, - кнопка внутри ячейки. Если вы хотите увидеть мой код, так что это может помочь вам ответить на этот вопрос, вот код:Как поместить кнопку внутри ячейки таблицы?

using System; 
using System.Collections.Generic; 
using System.Text; 
using Foundation; 
using UIKit; 

namespace TableView 
{ 
public class TableSource : UITableViewSource 
{ 
    string[] tableItems; 
    string cellIdentifier = "TableCell"; 



    public TableSource (string[] items) 
    { 
     tableItems = items; 
    } 

    public override nint RowsInSection(UITableView tableview, nint section) 
    { 
     return tableItems.Length; 
    } 
    public override void RowSelected(UITableView tableView, NSIndexPath indexPath) 
    { 
     new UIAlertView("Alert", "You selected: " + tableItems[indexPath.Row], null, "Next Site", null).Show(); 
     tableView.DeselectRow(indexPath, true); 
    } 
    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
    { 
     UITableViewCell cell = tableView.DequeueReusableCell(cellIdentifier); 
     if (cell == null) 
      cell = new UITableViewCell(UITableViewCellStyle.Subtitle, cellIdentifier); 
     cell.TextLabel.Text = tableItems[indexPath.Row]; 

     if(indexPath.Row > -1) 
      cell.DetailTextLabel.Text = tableItems[indexPath.Row - 0]; 



      return cell; 
    } 
} 
} 

Вот код для ViewController, если это необходимо.

+0

Если у вас что-то не так с ответом, вы можете удалить его или отредактировать. – AnonymUser

ответ

0

Создайте пользовательский TableViewCell и добавьте туда кнопку. Чем использовать этот пользовательский TableViewCell в методе GetCell.

class MyButtonTableViewCell : UITableViewCell 
{ 
    public UIButton MyButton { get; private set; } 

    public MyButtonTableViewCell() : base(UITableViewCellStyle.Default, "MyButtonCell") 
    { 
     MyButton = new UIButton(UIButtonType.System); 
     MyButton.Frame = ContentView.Bounds; 
     MyButton.SetTitle("My Title", UIControlState.Normal); 

     ContentView.AddSubview(MyButton); 
    } 
} 


public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath) 
{ 
    UITableViewCell cell = tableView.DequeueReusableCell("MyButtonCell"); 

    if (cell == null) 
     cell = MyButtonTableViewCell(); 

     return cell; 
} 
+0

Как создать пользовательский tableviewcell? Я имею в виду, могу ли я поместить этот код в tableviewsource? – AnonymUser

+0

В моем ответе приведен пример создания пользовательской ячейки. См. Класс MyButtonTableViewCell. Вы можете поместить этот класс в свой TableViewSource. –

+0

хорошо спасибо за ваш ответ – AnonymUser

0

Прежде всего вам нужно добавить тег к кнопке, так что вы можете увидеть, что кнопка нажата внутри cellForRowAtIndexPath функции, как это:

cell.Button.tag = indexPath.row 

Тогда внутри IBAction вы можете см., что именно была нажата кнопка:

@IBAction func buttonPressed(sender: AnyObject) 
{ 
    let button = sender as! UIButton 
    let index = NSIndexPath(forRow: button.tag, inSection: 0) 
} 
Смежные вопросы