我有一個預填充的UITableViewCell
在哪個單元有一個標籤和UITextField
。假設我有一個標籤爲「map」的單元格,並且我想在該單元格上添加按鈕,我該怎麼做?
我試過了,但這不起作用。
下面是代碼:如何添加UITableViewCell上的按鈕
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"rateSheetCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (indexPath.row == 0)
{
((UILabel*)[cell viewWithTag:100]).text = @"Title";
((UITextField*)[cell viewWithTag:101]).placeholder = @"Title";
((UITextField*)[cell viewWithTag:101]).text = arrayResourceColumns[indexPath.section][indexPath.row][@"resource_title"];
((UITextField*)[cell viewWithTag:101]).keyboardType = UIKeyboardTypeDefault;
}
else
{
if([arrayResourceColumns[indexPath.section][indexPath.row][@"label"] isEqualToString:@"OT ($)"]){
NSInteger desiredLabel = (NSInteger)(arrayResourceColumns[indexPath.section][indexPath.row][@"label"]);
//NSString *desiredLabel = arrayResourceColumns[indexPath.section][indexPath.row][@"label"];
NSLog(@"Label is>>>>--------%ld", (long)desiredLabel);
if (indexPath.row == desiredLabel) {
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(0, 0, 50, 50)];
}
}
((UILabel*)[cell viewWithTag:100]).text = arrayResourceColumns[indexPath.section][indexPath.row][@"label"];
((UITextField*)[cell viewWithTag:101]).placeholder = ((UILabel*)[cell viewWithTag:100]).text;
((UITextField*)[cell viewWithTag:101]).text = arrayResourceColumns[indexPath.section][indexPath.row][@"attribute_value"];
if (indexPath.row == [arrayResourceColumns[indexPath.section] count] - 1)
{
((UITextField*)[cell viewWithTag:101]).enabled = NO;
}
}
}
看起來你使用標籤100和101來訪問單元格的子視圖。但是你把它們添加到單元格的位置?他們應該添加在'cell == nil'的情況下順便說一句。 –
@Khanh標籤是textlabel和textfield,textlabel和textfield是在故事板的uitableviewcell原型中添加的 – Ammad
@ user1699419更好的是,您可以使用自己的自定義表格視圖單元格。在這裏你可以添加像UIButton,UILabel和UITextField等子視圖...你有解決方案嗎? –