2016-06-23 2 views
1

저는이 말을 처음 접했으므로 그 일을하는 데 어려움이 있습니다. 타이틀은 셀 안에있는 버튼처럼 내 목표도 들어 맞 춥니 다. 이것은에 필요한 경우 여기에서의 ViewController 코드입니다어떻게하면 단추를 tableview 셀 안에 넣을 수 있습니까?

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; 
    } 
} 
} 

: 당신이 당신이이 질문에 대답하기 위해 도움이 될 수 있도록, 내 코드를보고 싶다면, 여기에 코드입니다.

당신이이 같은 cellForRowAtIndexPath 함수 내에서 누르고있는 어떤 버튼을 볼 수 있도록 버튼에 태그를 추가 할 필요가 모든
+0

답변에 문제가있는 경우이를 삭제하거나 편집 할 수 있습니다. – AnonymUser

답변

0

사용자 정의 TableViewCell을 만들고 거기에 버튼을 추가하십시오. GetCell 메서드에서 해당 사용자 지정 TableViewCell을 사용하십시오.

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

첫째 :

다음
cell.Button.tag = indexPath.row 

IBAction를 당신이 할 수있는 내부 다음과 같이 어떤 행의 버튼이 눌려 졌는지 정확히 확인하십시오.

@IBAction func buttonPressed(sender: AnyObject) 
{ 
    let button = sender as! UIButton 
    let index = NSIndexPath(forRow: button.tag, inSection: 0) 
} 
관련 문제