就地编辑UITableViewCell中的文本?
问题描述:
我知道这是一个非常简单的问题,但我很难在一个简单的地方找到它,因为在这个地方没有太多的其他事情可以分离出特定的行为。就地编辑UITableViewCell中的文本?
我知道我需要添加一个UITextField作为UITableViewCell的子视图,以使其可编辑。做这个的最好方式是什么? (我在UICatalog中看到一个例子,但是我对它在字典中做的所有额外的事情感到困惑。)
再次,请原谅这个问题的无知。一个指向示例代码或屏幕截图的指针将不胜感激。
由于提前,
艾伦
答
您可以创建自定义的UITableViewCell,并把任何你在它想要的。 然后你只需要在你的UITableView中使用它。 如果你担心为你的文本字段使用UITextField委托来管理所有的委托方法,恕我直言,我会把它放在处理UITableView的UIViewController中,以便你可以在编辑完文本字段后更新数据源。
答
首先,定义约UITextField
。
txtField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 320, 39)];
txtField.autoresizingMask=UIViewAutoresizingFlexibleHeight;
txtField.autoresizesSubviews=YES;
txtField.layer.cornerRadius=10.0;
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setPlaceholder:@"Type Data Here"];
然后把代码写在下面cellForRowAtIndexPath
方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
[cell addSubview:txtField];
return cell;
}
不宜子视图if语句内添加,因此它可以是细胞的出队的一部分吗? – 2012-11-02 22:44:56