use*_*963 1 iphone objective-c uitableview uilabel ios
我将两个添加UILabels到a UITableViewCell,然后为该单元格命名和编号标签.当我重新加载数据时UITableView(无论是从表格视图添加还是删除联系人),标签中的数据都是重叠的,这意味着它不会重新加载标签,直到重新加载整个视图.
有人可以帮忙吗?
码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UILabel *name;
UILabel *number;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
// NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
// [sortDescriptor release];
}
CGRect Label1Frame = CGRectMake(10, 10, 140, 25);
name = [[UILabel alloc] initWithFrame:Label1Frame];
name.tag = 1;
[name setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:name];
CGRect Label2Frame = CGRectMake(150, 10, 140, 25);
number = [[UILabel alloc] initWithFrame:Label2Frame];
number.tag = 1;
[number setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:number];
name.text = [NSString stringWithFormat:@"%@",[names objectAtIndex:indexPath.row]];
number.text = [NSString stringWithFormat:@"%@",[phonenumbers objectAtIndex:indexPath.row]];
[name release];
[number release];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
您应该在单元格初始化中移动标签创建代码,然后稍后通过标记引用它们,如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
//NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
// NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
// [sortDescriptor release];
CGRect Label1Frame = CGRectMake(10, 10, 140, 25);
UILabel *name = [[UILabel alloc] initWithFrame:Label1Frame];
name.tag = 1;
[name setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:name];
[name release];
CGRect Label2Frame = CGRectMake(150, 10, 140, 25);
UILabel *number = [[UILabel alloc] initWithFrame:Label2Frame];
number.tag = 2;
[number setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:number];
[number release];
}
UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
UILabel *numberLabel = (UILabel *)[cell viewWithTag:2];
name.text = [NSString stringWithFormat:@"%@",[names objectAtIndex:indexPath.row]];
number.text = [NSString stringWithFormat:@"%@",[phonenumbers objectAtIndex:indexPath.row]];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3248 次 |
| 最近记录: |