[英]how to determine the sections for cell in UITableView - objective-c
[英]how to put button in cell of UItableview in objective-c?
我有一个数组要显示在tableview中。我需要在最后一个单元格中有一个按钮。意味着有20个元素的数组我需要在第21个单元格中有一个按钮如何做到这一点。我还需要对该按钮进行操作..如何做到这一点告诉我代码...谢谢。
您将不得不管理比数组元素数量多一个单元。 因此,请确保在表视图数据源实现中进行以下修改:
// return the correct row count
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [theArray count] + 1;
}
// detect the last cell and add it a subview
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
...
...
// detect the last row
if(indexPath.row == [theArray count]) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
// set the frame and title you want
[b setFrame:CGRectMake(0, 0, 50, 50)];
[b setTitle:@"button" forState:UIControlStateNormal];
// set action/target you want
[b addTarget:self
action:@selector(theActionYouWant:)
forControlEvents:UIControlEventTouchDragInside];
[cell addSubview:b];
}
else {
// configure a classic cell
}
return cell;
}
// the action method
-(void)theActionYouWant:(id)sender {
// handle the action
}
作为替代方案,为什么不只使用整个单元来执行动作? 使用相同的机制来管理一个以上的单元,为其设置一个自定义标签,然后当您检测到该单元被选中时,发送一条消息:
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if([indexPath row] == [theArray count]) {
// the extra cell is selected, send a message
}
else {
// others cells selection handler
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.